Jump to content

haris244808

Members
  • Posts

    110
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

haris244808's Achievements

Member

Member (2/5)

0

Reputation

  1. Thnx for replying Here is the form: <form action="page that have the function" method="post" enctype="multipart/form-data" > <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $placeGallery->max_photo_size; ?>" /> <input name="pg_photo_name[]" type="file" style="width:92%;" multiple /> <form>
  2. Im trying to upload multiple pictures, but the code im using inserts only the last picture selected... can anyone tell me where i have my mistake pls here is the code that gets the file from form : if($placeGallery->upload($_FILES['pg_photo_name'])){//success //redirect to any page } else{//failure //bllallalla } } and here is the function im calling: public function upload($placeGalleryPhoto){ foreach($placeGalleryPhoto['name'] as $i => $name) { if($name[$i] == ""){ $this->errors[] = "No file was uploaded."; return false; } //Report what PHP says went wrong elseif($placeGalleryPhoto['error'][$i] != 0){ $this->errors[] = $this->file_errors[$placeGalleryPhoto['error'][$i]]; return false; } else{ $this->temp_path = $placeGalleryPhoto['tmp_name'][$i]; $this->extension = strtolower(strrchr($placeGalleryPhoto['name'][$i], '.')); $filename = //some random names; $this->photo_name = $filename . $this->extension; // Can't save without filename and temp location if(empty($this->photo_name) || empty($this->temp_path)){ $this->errors[] = "The file location was not available."; return false; } //target directory + picture name $uploadDir = $this->dir . $this->photo_name; if(move_uploaded_file($this->temp_path, $uploadDir)){ //sql query check } else{ $this->error[] = "Could not copy picture."; return false; } } } }
  3. i changed also my code trying with for loop... it uploads now only the last picture.... here is the code: <?php require_once('../includes/sql_connection.class.php'); require_once('includes/functions.php'); class PlaceGallery{ protected $db_table = "placegallery"; public $id; public $photo_name; //place gallery picture name public $place_id; //place_id of placedetails got from selectbox public $max_photo_size = 1048576; // 1MB public $max_files = 5; //max files allowed to select at once private $temp_path; //Server hold here the file before moving to its directory. public $dir = "../images/places/"; public $thumbsDir = "../images/places/thumbs/"; public $extension;//extension of place_main_photo_name public $valid_extensions = array('.png', '.gif', '.jpg', '.jpeg'); public $errors = array(); //all error messages are stored here for display protected $file_errors = array( // http://www.php.net/manual/en/features.file-upload.errors.php UPLOAD_ERR_OK => "No errors.", UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.", UPLOAD_ERR_FORM_SIZE => "Picture is too Large. (Max = 1MB)", UPLOAD_ERR_PARTIAL => "Partial upload.", UPLOAD_ERR_NO_FILE => "No File. Please insert a Picture", UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.", UPLOAD_ERR_CANT_WRITE => "Can't write to disk.", UPLOAD_ERR_EXTENSION => "File upload stopped by extension." ); public function placeGallery_upload($placeGalleryPhoto){ for ($i = 0; $i < count($placeGalleryPhoto['name']); $i++) { // Perform error checking on the form parameters if($placeGalleryPhoto['name'][$i] == ""){ // error: nothing uploaded or wrong argument usage $this->errors[] = "No file was uploaded."; return false; } elseif($placeGalleryPhoto['error'][$i] != 0){ // error: placeGalleryPhoto what PHP says went wrong $this->errors[] = $this->file_errors[$placeGalleryPhoto['error'][$i]]; return false; } else{ // Set object attributes to the form parameters. $this->temp_path = $placeGalleryPhoto['tmp_name'][$i]; $this->photo_name = $placeGalleryPhoto['name'][$i]; // Can't save without filename and temp location if(empty($this->photo_name) || empty($this->temp_path)){ $this->errors[] = "The file location was not available."; return false; } //target directory + picture name $uploadDir = $this->dir . $this->photo_name; //thumbs directory + picture name $uploadThumbDir = $this->thumbsDir .$this->photo_name; // Make sure a file doesn't already exist in the target location if(file_exists($uploadDir) || file_exists($uploadThumbDir)){ $this->errors[] = "The file: ".$this->photo_name." already exists."; return false; } // Move file into the directory. Insert picture name and other values to the database if(move_uploaded_file($this->temp_path, $uploadDir)){ if(copy($uploadDir, $uploadThumbDir)){ if($this->create()) { // We are done with temp_path, the file isn't there anymore unset($this->temp_path); return true; } else{ $this->error[] = "Could not copy picture."; return false; } } else{ $this->error[] = "Could not upload picture.Check read/write permissions on directory"; return false; } } return true; } }//end for loop }//end of upload function public function create(){ global $db; $sql = "INSERT INTO ".$this->db_table."(place_id, pg_photo_name, pg_date) VALUES( '".$db->escape_value($this->place_id)."', '".$db->escape_value($this->photo_name)."', now())"; if($db->query($sql)){ $this->id = $db->insert_id(); //store also id to $admin_id object. header("Location: adminPanel.php?msg=pg_success"); }//end of if query is successfull else{ $this->error[] = "Error in inserting your informations to database"; return false; } }//end of create function public function delete(){ global $db; $uploadDir = $this->dir . $this->photo_name; $uploadThumbDir = $this->thumbsDir . $this->photo_name; if(unlink($uploadDir)){ if(unlink($uploadThumbDir)){ $sql = $db->query("DELETE FROM ".$this->db_table." WHERE place_id=". $db->escape_value($this->place_id). " LIMIT 1"); return ($db->affected_rows() == 1) ? true : false; } else{ return false; } } else{ return false; } }//end of delete function public function find_by_id($id=0){ global $db; $sql = $db->query( "SELECT * FROM ".$this->db_table." WHERE place_id = ".$db->escape_value($id). " LIMIT 1 "); $result = $db->fetch_assoc($sql); $this->id = $id; $this->photo_name = $result[pg_photo_name]; return $result; } //end of find_by_id function }//End of class PlaceGallery $placeGallery = new PlaceGallery(); ?>
  4. thank u man for Naming Convention. It really helps>> about the if(!in_array($extension, $this->valid_extensions)) i have an array $valid extensions which holds the extentions allowed for upload... I thought checking this way itll be good for security (as i see im wrong). So How can i manage this validation to prevent from any attack ??
  5. im not familiar with CURL() . i saw it a little, it looked hard to me... I need to modify this function (that i posted) to validate and upload multiple images only... I cannot figure it out... if someone can help me pls do so... btw: thnx darkfreaks
  6. im trying to upload multiple images to a sppecified directory and the image names in db. I am not getting any error, but pictures doesnt move to the specified directory also the name of the picture in the db is not appearing..> any help will be greatfull .. THNX Here is the function that im using for upload: public function placeGallery_upload($placeGalleryPhoto){ foreach($placeGalleryPhoto['tmp_name'] as $key => $tmp_name){ // Perform error checking on the form parameters if(!$tmp_name || empty($tmp_name) || !is_array($tmp_name)){ // error: nothing uploaded or wrong argument usage $this->errors[] = "No file was uploaded."; return false; } elseif($tmp_name['error'] != 0){ // error: placeGalleryPhoto what PHP says went wrong $this->errors[] = $this->file_errors[$tmp_name['error']]; return false; } else{ // Set object attributes to the form parameters. $this->placeGallery_temp_path = $tmp_name['tmp_name']; $this->extension = strtolower(strrchr($tmp_name['name'], '.')); $today = getdate(); $filename = $_SESSION['random']."-".$today; $this->placeGallery_photo_name = $filename . $this->extension . "[".$key."]" ; // Can't save without filename and temp location if(empty($this->placeGallery_photo_name) || empty($this->placeGallery_temp_path)){ $this->errors[] = "The file location was not available."; return false; } // Check file extension if(!in_array($extension, $this->valid_extensions)){ $this->errors[] = "File format is invalid. Can only be png, gif, jpeg and jpg"; return false; } //target directory + picture name $placeGallery_UploadDir = $this->placeGallery_Dir . $this->placeGallery_photo_name; //thumbs directory + picture name $placeGallery_UploadThumbDir= $this->placeGallery_thumbsDir . $this->placeGallery_photo_name; // Make sure a file doesn't already exist in the target location if(file_exists($placeGallery_UploadDir) || file_exists($placeGallery_UploadThumbDir)){ $this->errors[] = "The file: ".$this->placeGallery_photo_name." already exists."; return false; } // Move file into the directory. Insert picture name and other values to the database if(move_uploaded_file($this->placeGallery_temp_path, $placeGallery_UploadDir)){ if(copy($placeGallery_UploadDir, $placeGallery_UploadThumbDir)){ if($this->placeGallery_create()) { // We are done with pg_temp_path, the file isn't there anymore unset($this->pg_temp_path); return true; } else{ $this->error[] = "Could not copy picture."; return false; } } else{ $this->error[] = "Could not upload picture.Check read/write permissions on directory"; return false; } } return true; } }//end of foreach loop }//end of placeGallery_upload function And here is the code where im getting the values from: if(isset($_POST['populatePlaceGallery'])){ //trim() removes whitespaces and other predefined characters from both sides of a string. $placeGallery->p_id = trim($_POST['place']); if($placeGallery->placeGallery_upload($_FILES['pg_photo_name'])){//success } else{//failure //The join() function returns a string from the elements of an array. $message = join("<br />", $placeGallery->errors); } }
  7. i figured it out... it was not getting the filename corrrectly thnx anyway
  8. of course that i call that function to another page
  9. i cannot edit now... Im Sorry for that mistake
  10. here are the configs in php.ini: i debuged and it shows no error. I am using classess. I get a file name and directory to another function and assign them to those values inside unlink() function error_reporting = E_ALL | E_STRICT ; error_reporting ; Default Value: E_ALL & ~E_NOTICE ; Development Value: E_ALL | E_STRICT ; Production Value: E_ALL & ~E_DEPRECATED ; display_errors ; Default Value: On ; Development Value: On ; Production Value: Off
  11. im trying to delete 2 files but the unlink function doesnt delete them any suggestion here is function im using: public function place_delete(){ $cityPlace_UploadDir = $this->cityPlace_Dir . $this->place_main_photo_name; $cityPlace_UploadThumbDir = $this->cityPlace_thumbsDir . $this->place_main_photo_name; if(unlink($cityPlace_UploadDir)){ if(unlink($cityPlace_UploadThumbDir)){ //DELETE some sql values return (mysql_affected_rows() == 1) ? true : false; } else{ return false; } } else{ return false; } }
  12. We know. We know very very well. That's why my code example was more than one line. It was, in fact, three lines. One of those lines was how you can check to see if a string is a valid floatvalue. also note the existence of is_numeric(), which works on strings. Also also note that latitude and longitude are correctly expressed in minutes and seconds, as in -64'38" yeah yeah i know that ur code was in three lines: $test = "132.3"; if(var_dump(floatval($test)) != $test){ echo " Fail";} else{ echo "Pass";} is_numeric() it just checks if its a number or not, so in integers also itll return true (i need to check if its double ) for lat and long im getting double values to display the propper postion in map
  13. Please reread ManiacDan's response. Don't just skim over it - really read it. And pay particular attention to the code examples. i red it well man: php > $a = "12.34"; php > var_dump(is_float($a)); bool(false) as u can see if there is a float in string qoute it returns false... This is what im saying...even when u write a float in ur input gettinh it with$_POST[] , it gets as a string so it doesnt work... if u dont believe try the code i gave to u? or try this for simplicity: $test = "132.3"; if(!is_float($test)){ echo " Fail";} else{ echo "Pass";} it seems that i frist need to convert the string to float than check it
  14. What makes you think this? That's exactly the purpose of those functions: php > $a = "12.34"; php > var_dump(is_float($a)); bool(false) php > var_dump(floatval($a)); float(12.34) php > var_dump(floatval($a) == $a); bool(true) php > yes i know thaose functions works well .. But as im getting input with $_POST[]... it seems that it gets as a string so when i check, it doesn work : here is the code: if(!is_float($this->place_latitude) || !is_float($this->place_longitude)){ $this->errors[] = "The Latitude and Longitude must be a number(double)"; return false; } here is the html form for only lattitude: <td><label>Longitude: </label></td> <td><input name="p_latitude" type="text" placeholder="Place latitude"></td> and i get it with: $newPlace->place_latitude = trim($_POST['p_latitude']); so when i type a numer (ex: 12321) which in this case is an integer. It returns true instead of showing the error mesage (returning false)
  15. Hi there, im trying to validate an input if its float or not... As im getting the value with $_POST['smth']; the value its considered as string, so is_float() function doesnt work. I tried to convert the string also with floatval() function that doesnt work either Is there any suggestion on how to get done with this?? THNX
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.