mike12255 Posted October 1, 2011 Share Posted October 1, 2011 Im trying to use move_uploaded_file() to move an uploaded file from the temp directory to a different directory and when I run this function I get this error: Warning: move_uploaded_file(/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phprQRBDe' to '/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf' in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238 There was an error uploading the file, please try again! I have no idea why, below is my code: <?php $target_path = $uploadLocation . basename( $_FILES['upfile']['name']); $ext = end(explode('.', $_FILES['upfile']['name'])); function generatePassword ($length = { // start with a blank password $password = ""; // define possible characters - any character in this string can be // picked for use in the password, so if you want to put vowels back in // or add special characters such as exclamation marks, this is where // you should do it $possible = "_12346789abcdfghjkmnpqrtvwxyzABCDFGHJKLMNPQRTVWXYZ"; // we refer to the length of $possible a few times, so let's grab it now $maxlength = strlen($possible); // check for length overflow and truncate if necessary if ($length > $maxlength) { $length = $maxlength; } // set up a counter for how many characters are in the password so far $i = 0; // add random characters to $password until $length is reached while ($i < $length) { // pick a random character from the possible ones $char = substr($possible, mt_rand(0, $maxlength-1), 1); // have we already used this character in $password? if (!strstr($password, $char)) { // no, so it's OK to add it onto the end of whatever we've already got... $password .= $char; // ... and increase the counter by one $i++; } } // done! return $password; } $filename = generatePassword(); $filename = $filename.".".$ext; $filelocation = "/home/mikeh/public_html/uzErUpl0ds/".$filename; $allowed = "doc docx rtf txt pdf xls"; $allow = explode (" ",$allowed); if (in_array($ext,$allow)){ if(move_uploaded_file($_FILES['upfile']['tmp_name'], $filelocation)) { //TODO: Insert date regex format and make the catagory selection auto// $uid = $_POST['catagory']; $name =$_POST['title']; $author = $_POST['author']; $cat = $_POST['code']; $dt1=date("Y-m-d"); $size = filesize($filelocation); die($filelocation); $query = "INSERT INTO newnotes (uid, name, catagory, location, author, date, filesize) VALUES ('$uid', '$name', '$cat', '$filename', '$author', '$dt1','size')"; //die ($query); $res = mysql_query($query) or die (mysql_error()); echo "The file: ". basename( $_FILES['upfile']['name']). " has been uploaded! It will be reviewed and if possible you will be granted credits within the next 24 hours."; } else{ echo "There was an error uploading the file, please try again!"; } }else{ echo "You have tried to upload an file type that we do not allow. The current allowed types of files are, ".implode(',',$allow).". If you are trying to upload a note with a diffrent file extension please use <a href=\"contact.php\">this form</a> to let us know so we can fix our website to allow this type of file in the future."; } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 1, 2011 Share Posted October 1, 2011 Does your <form> tag include the enctype="multipart/form-data" attribute? Also, you aren't checking to see whether $_FILES['upfile']['error'] contains a value that would indicate an error occurred before attempting to move the file. Quote Link to comment Share on other sites More sharing options...
mike12255 Posted October 1, 2011 Author Share Posted October 1, 2011 The form does have that attribute and $_FILES['image']['error'] equals 0 Quote Link to comment Share on other sites More sharing options...
xyph Posted October 1, 2011 Share Posted October 1, 2011 Check if the file actually exists using file_exists or physically checking. It may be a permissions issue but I doubt it. Quote Link to comment Share on other sites More sharing options...
Drummin Posted October 1, 2011 Share Posted October 1, 2011 Make sure your form has a MAX_FILE_SIZE input large enough to hold file in temp. <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 1, 2011 Share Posted October 1, 2011 If the file size was an issue, it would show up in $_FILES['userfile']['error']. UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini. UPLOAD_ERR_FORM_SIZE Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2011 Share Posted October 1, 2011 Warning: move_uploaded_file(/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phprQRBDe' to '/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf' in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238 You need to set the destination path to an actual folder that exists in the following statement - $filelocation = "/home/mikeh/public_html/uzErUpl0ds/".$filename; Quote Link to comment Share on other sites More sharing options...
Drummin Posted October 1, 2011 Share Posted October 1, 2011 Pikachu2000, you are right IF a person adds $_FILES['userfile']['error'] to their page to get error number. Otherwise they would see a message like was posted. Warning: move_uploaded_file(/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phprQRBDe' to '/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf' in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238 There was an error uploading the file, please try again! See I posted here regarding a similar issue but received no replies. http://www.phpfreaks.com/forums/index.php?topic=342959.msg1617984#msg1617984 After searching the web I found the "$_FILES['userfile']['error']" to give me the error number and then looked it up. Until then, all I was seeing was the "failed to open stream" warning. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2011 Share Posted October 1, 2011 @Drummin, for what you are suggesting, the actual error messages you were getting would have indicated that the source file did not exist. Not that the destination folder did not exist. Not the exact same error messages, so not the exact same problem. You cannot generalize that an error message that contains "failed to open stream" always means the same thing. Quote Link to comment Share on other sites More sharing options...
Drummin Posted October 1, 2011 Share Posted October 1, 2011 PFMaBiSmAd you are probably correct in that I don't have the warning I was getting to reference. In any case, adding $_FILES['userfile']['error'] would give you the error number to point out exact problem. Just trying to help. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 1, 2011 Share Posted October 1, 2011 Pikachu2000, you are right IF a person adds $_FILES['userfile']['error'] to their page to get error number. Otherwise they would see a message like was posted. Warning: move_uploaded_file(/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phprQRBDe' to '/home/mikeh/public_html/uzErUpl0ds/jx8zmPQW.rtf' in /Applications/XAMPP/xamppfiles/htdocs/fileupload.php on line 238 There was an error uploading the file, please try again! See I posted here regarding a similar issue but received no replies. http://www.phpfreaks.com/forums/index.php?topic=342959.msg1617984#msg1617984 After searching the web I found the "$_FILES['userfile']['error']" to give me the error number and then looked it up. Until then, all I was seeing was the "failed to open stream" warning. The form does have that attribute and $_FILES['image']['error'] equals 0 Quote Link to comment Share on other sites More sharing options...
mike12255 Posted October 3, 2011 Author Share Posted October 3, 2011 hey thanks for all the replies guys, was gone for the weekend, that folder exists, ill double check the path though, thx. Quote Link to comment 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.