eldan88 Posted June 19, 2013 Share Posted June 19, 2013 Hey, I am trying to upload a file to my local machine, and every time I try, I keep getting the following error message. Warning: move_uploaded_file(Applications/XAMP/xamppfiles/htdocs/photo_gallery/public/images/flowers.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/photo_gallery/includes/photograph.php on line 82Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phpBqF50S' to 'Applications/XAMP/xamppfiles/htdocs/photo_gallery/public/images/flowers.jpg' in /Applications/XAMPP/xamppfiles/htdocs/photo_gallery/includes/photograph.php on line 82 I'm trying to figure out what does it mean when it says "No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/photo_gallery/includes/photograph.php on line 82" Below is the full code I am trying to use, followed by the upload form. I have commented out where line number 82 is on the photograph class. Does anyone have any suggestions on how to fix it it? File upload page(file_upload.php) <?php require_once("../../includes/functions.php"); require_once("../../includes/photograph.php"); ?> <?php $max_file_size = 1048576; $message = ""; if(isset($_POST['submit'])) { $photo = new Photograph(); $photo->caption = $_POST['caption']; $photo->attach_file($_FILES['file_upload']); if($photo->save()) { // Success $message = "Photograph uploaded successfully."; } else { // Failure $message = join("<br />", $photo->errors); } } ?> <?php echo output_message($message); ?> <form action="photo_upload.php" enctype="multipart/form-data" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" /> <p><input type="file" name="file_upload" /></p> <p>Caption: <input type="text" name="caption" value="" /></p> <input type="submit" name="submit" value="Upload" /> </form> Photograph Class that the file_upload.php uses <?php require_once("database.php"); //site_root :/Applications/XAMP/xamppfiles/htdocs/photo_gallery/public/images class Photograph { protected static $table_name = "photographs"; protected static $db_fields=array('id','filename','type','size','caption'); public $id; public $filename; public $type; public $size; public $caption; private $tmp_path; protected $upload_dir="images"; public $errors = array(); protected $upload_errors = array( UPLOAD_ERR_OK => "No Errors", UPLOAD_ERR_INI_SIZE => "Larger than upload file size", UPLOAD_ERR_FORM_SIZE => "Larger than from max upload size", UPLOAD_ERR_PARTIAL => "Partial upload", UPLOAD_ERR_NO_FILE => "No File", UPLOAD_ERR_NO_TMP_DIR => "No temporary directory", UPLOAD_ERR_CANT_WRITE => "Can't Write to disk", UPLOAD_ERR_EXTENSTON => "File Upload stopped by extension" ); // Pass in $_FILE(['uploaded_file']) as an argument public function attach_file($file) { // Perform error checking on the form parameters if(!$file || empty($file) || !is_array($file)) { // error: nothing uploaded or wrong argument usage $this->errors[] = "No file was uploaded."; return false; } elseif($file['error'] != 0) { // error: report what PHP says went wrong $this->errors[] = $this->upload_errors[$file['error']]; return false; } else { // Set object attributes to the form parameters. $this->temp_path = $file['tmp_name']; $this->filename = basename($file['name']); $this->type = $file['type']; $this->size = $file['size']; // Don't worry about saving anything to the database yet. return true; } } public function save() { // A new record won't have an id yet. if(isset($this->id)) { // Really just to update the caption $this->update(); } else { // Make sure there are no errors // Can't save if there are pre-existing errors if(!empty($this->errors)) { return false; } // Make sure the caption is not too long for the DB if(strlen($this->caption) > 255) { $this->errors[] = "The caption can only be 255 characters long."; return false; } // Can't save without filename and temp location if(empty($this->filename) || empty($this->temp_path)) { $this->errors[] = "The file location was not available."; return false; } $target_path = "Applications/XAMP/xamppfiles/htdocs/photo_gallery/public/".$this->upload_dir."/".$this->filename; if(file_exists($target_path)) { $this->errors[] = "The file {$this->filename} already exists"; return false; } if(move_uploaded_file($this->temp_path, $target_path)) { // Line # 82 where error starts if($this->create()) { unset($this->temp_path); return true; } } else { $this->errors[] = "The file upload failed, Possibly due to incorrect permessions on the folder"; } //Attempt to move the file //Save the corresponding entry to the database } } //comment database methods public static function find_all() { return self::find_by_sql("SELECT * FROM ".self::$table_name); } public static function find_by_id($id=0) { $result_array = self::find_by_sql("SELECT * FROM ".self::$table_name." WHERE id={$id} LIMIT 1"); return !empty($result_array) ? array_shift($result_array) : false; } public static function find_by_sql($sql="") { global $database; $result_set = $database->query($sql); $object_array = array(); while ($row = $database->fetch_array($result_set)) { $object_array[] = self::instantiate($row); } return $object_array; } private static function instantiate($record) { // Could check that $record exists and is an array $object = new self; // Simple, long-form approach: // $object->id = $record['id']; // $object->username = $record['username']; // $object->password = $record['password']; // $object->first_name = $record['first_name']; // $object->last_name = $record['last_name']; // More dynamic, short-form approach: foreach($record as $attribute=>$value){ if($object->has_attribute($attribute)) { $object->$attribute = $value; } } return $object; } private function has_attribute($attribute) { // We don't care about the value, we just want to know if the key exists // Will return true or false return array_key_exists($attribute, $this->attributes()); } protected function attributes() { // return an array of attribute names and their values $attributes = array(); foreach(self::$db_fields as $field) { if(property_exists($this, $field)) { $attributes[$field] = $this->$field; } } return $attributes; } protected function sanitized_attributes() { global $database; $clean_attributes = array(); // sanitize the values before submitting // Note: does not alter the actual value of each attribute foreach($this->attributes() as $key => $value){ $clean_attributes[$key] = $database->escape_value($value); } return $clean_attributes; } public function create() { global $database; // Don't forget your SQL syntax and good habits: // - INSERT INTO table (key, key) VALUES ('value', 'value') // - single-quotes around all values // - escape all values to prevent SQL injection $attributes = $this->sanitized_attributes(); $sql = "INSERT INTO ".self::$table_name." ("; $sql .= join(", ", array_keys($attributes)); $sql .= ") VALUES ('"; $sql .= join("', '", array_values($attributes)); $sql .= "')"; if($database->query($sql)) { $this->id = $database->insert_id(); return true; } else { return false; } } public function update() { global $database; // Don't forget your SQL syntax and good habits: // - UPDATE table SET key='value', key='value' WHERE condition // - single-quotes around all values // - escape all values to prevent SQL injection $attributes = $this->sanitized_attributes(); $attribute_pairs = array(); foreach($attributes as $key => $value) { $attribute_pairs[] = "{$key}='{$value}'"; } $sql = "UPDATE ".self::$table_name." SET "; $sql .= join(", ", $attribute_pairs); $sql .= " WHERE id=". $database->escape_value($this->id); $database->query($sql); return ($database->affected_rows() == 1) ? true : false; } public function delete() { global $database; // Don't forget your SQL syntax and good habits: // - DELETE FROM table WHERE condition LIMIT 1 // - escape all values to prevent SQL injection // - use LIMIT 1 $sql = "DELETE FROM ".self::$table_name; $sql .= " WHERE id=". $database->escape_value($this->id); $sql .= " LIMIT 1"; $database->query($sql); return ($database->affected_rows() == 1) ? true : false; // NB: After deleting, the instance of User still // exists, even though the database entry does not. // This can be useful, as in: // echo $user->first_name . " was deleted"; // but, for example, we can't call $user->update() // after calling $user->delete(). } }// End of photograph class ?> Quote Link to comment https://forums.phpfreaks.com/topic/279369-need-help-uploading-a-file/ Share on other sites More sharing options...
eldan88 Posted June 19, 2013 Author Share Posted June 19, 2013 I actually went back to check the path to the $target_file and there was a minor typo. However, after I have fixed it its still giving me that error message. Any way can debug this to see whats going on? Quote Link to comment https://forums.phpfreaks.com/topic/279369-need-help-uploading-a-file/#findComment-1436939 Share on other sites More sharing options...
Solution mac_gyver Posted June 19, 2013 Solution Share Posted June 19, 2013 you are missing the leading / on the $target_path, so it is being treated as a path relative to the current script's folder, instead of an absolute file system path. you should be forming file system paths based on $_SERVER['DOCUMENT_ROOT'] . /the path within the document root folder/ so that you don't have hard-code paths floating around in your code that you have to change every time you move your script to a different server. after you write and test something like an oop class, you should never have to touch the code inside of it just because you changed where you are running that code. Quote Link to comment https://forums.phpfreaks.com/topic/279369-need-help-uploading-a-file/#findComment-1436940 Share on other sites More sharing options...
eldan88 Posted June 21, 2013 Author Share Posted June 21, 2013 you are missing the leading / on the $target_path, so it is being treated as a path relative to the current script's folder, instead of an absolute file system path. you should be forming file system paths based on $_SERVER['DOCUMENT_ROOT'] . /the path within the document root folder/ so that you don't have hard-code paths floating around in your code that you have to change every time you move your script to a different server. after you write and test something like an oop class, you should never have to touch the code inside of it just because you changed where you are running that code. Thank you very much! I added that and it now moved the uploaded file!! :happy-04: Quote Link to comment https://forums.phpfreaks.com/topic/279369-need-help-uploading-a-file/#findComment-1437165 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.