pritam79 Posted July 3, 2009 Share Posted July 3, 2009 (edited) I am running the following scripts in wamp, but i am unable to upload the selected files. The file which i am uploading is a jpeg of size 1.56 kb.This is the html form <html><body><form action="upload_file.php" method="post"enctype="multipart/form-data"><label for="file">Filename:</label><input type="file" name="file" id="file" /><br /><input type="submit" name="submit" value="Submit" /></form></body></html> This is the upload script <?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } }else { echo "Invalid file"; }?> After selecting a jpeg image the output i am getting is- Upload: pic.jpgType: image/jpegSize: 1.5634765625 KbTemp file: C:\wamp\tmp\phpD05E.tmpWarning: move_uploaded_file(upload/pic.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\wamp\www\eg\upload_file.php on line 25Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\phpD05E.tmp' to 'upload/pic.jpg' in C:\wamp\www\eg\upload_file.php on line 25Stored in: upload/pic.jpg I am also unable to find the uploaded image in upload/pic.jpeg. do i need to create the upload folder in tmp folder? Edited July 3, 2009 by pritam79 Link to comment Share on other sites More sharing options...
justsomeguy Posted July 3, 2009 Share Posted July 3, 2009 (edited) You need to create the destination folder before moving a file into it. Since it's telling you the destination doesn't exist you probably need to give it the full path instead of a relative path. You can also use this function to move an uploaded file: function handle_file_upload($options){ $input = !empty($options['input']) ? $options['input'] : trigger_error('handle_file_upload: No input name specified', E_USER_ERROR); $dest = !empty($options['dest_dir']) ? $options['dest_dir'] : trigger_error('handle_file_upload: No destination name specified', E_USER_ERROR); $dest_fname = !empty($options['dest_fname']) ? $options['dest_fname'] : ''; $overwrite = !empty($options['overwrite']) ? $options['overwrite'] : false; $mode = !empty($options['mode']) ? $options['mode'] : false; $allowed_ext = isset($options['allowed_ext']) && is_array($options['allowed_ext']) ? $options['allowed_ext'] : false; if (!is_dir($dest)) trigger_error('handle_file_upload: Destination directory does not exist', E_USER_ERROR); if (!isset($_FILES[$input])) trigger_error('handle_file_upload: The input file was not found', E_USER_ERROR); if ($_FILES[$input]['error'] > 0) { switch ($_FILES[$input]['error']) { case 1: case 2; return array('success' => false, 'error' => 'The uploaded file was too large.'); case 3: return array('success' => false, 'error' => 'The uploaded file was only partially received.'); case 4: return array('success' => false, 'error' => 'No file was uploaded.'); case 6: return array('success' => false, 'error' => 'Missing temporary folder.'); case 7: return array('success' => false, 'error' => 'Failed to write file to disk.'); case 8: return array('success' => false, 'error' => 'Invalid extension.'); } } if ($allowed_ext != false && !in_array(strtolower(array_pop(explode('.', $_FILES[$input]['name']))), $allowed_ext)) return array('success' => false, 'error' => 'That file type was not allowed.'); if ($dest_fname != '') $_FILES[$input]['name'] = $dest_fname; $_FILES[$input]['name'] = strtolower(basename($_FILES[$input]['name'])); if (!$overwrite) { $fname = $_FILES[$input]['name']; if (file_exists($dest . DIRECTORY_SEPARATOR . $fname)) { $chunks = explode('.', $fname); $ext = array_pop($chunks); $fname = implode('.', $chunks); $nr = 1; while (file_exists($dest . DIRECTORY_SEPARATOR . $fname . '.' . $ext)) $fname = $fname . '.' . $nr++; $_FILES[$input]['name'] = $fname . '.' . $ext; } } $target = $dest . DIRECTORY_SEPARATOR . $_FILES[$input]['name']; if (!move_uploaded_file($_FILES[$input]['tmp_name'], $target)) return array('success' => false, 'error' => 'The uploaded file could not be moved.'); if ($mode !== false) chmod($target, $mode); return array('success' => true, 'name' => $_FILES[$input]['name']);} ex: $file = handle_file_upload(array( 'input' => 'file', // name of the element in the $_FILES array 'dest' => dirname(__FILE__) . DIRECTORY_SEPARATOR . 'upload', // destination folder 'dest_fname' => 'upload.jpg', // destination filename (optional, else it uses the original name of the file) 'overwrite' => false, // false by default, true to overwrite an existing file, else it renames the new file 'mode' => 0644, // mode to change the new file to, optional 'allowed_ext' => array('jpg', 'jpeg', 'gif', 'png') // allowed extensions, optional));if (!$file['success']){ echo $file['error'];}else{ echo 'file uploaded as ' . $file['name'];} Edited July 3, 2009 by justsomeguy Link to comment Share on other sites More sharing options...
pritam79 Posted July 5, 2009 Author Share Posted July 5, 2009 Thanks justsomeguy, its working now Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now