timpickens Posted December 26, 2010 Share Posted December 26, 2010 For the life of me, I cannot get an uploaded file to move from the tmp folder to the uploads folder. I have made sure all permissions were set to allow for the transfer. $upfile points to the right folder and file. print_r($_FILES); yields the following: Array ( [userfile] => Array ( [name] => 02.jpg [type] => image/jpeg [tmp_name] => /tmp/phpClFyXP [error] => 0 => 121200 ) ) Any help would be greatly appreciated. HTML: <html> <head></head> <body> <form enctype="multipart/form-data" action="upload.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value = "2000000000"> Upload this file: <input name ="userfile" type="file"> <input type="submit" value="Send File"> </form> </body> </html> PHP: <html> <head> <title>Uploading...</title> </head> <body> <h1>Uploading file...</h1> <?php if ($_FILES['userfile']['error'] > 0) { echo 'Problem: '; switch ($_FILES['userfile']['error']) { case 1: echo 'File exceeded upload_max_filesize'; break; case 2: echo 'File exceeded max_file_size'; break; case 3: echo 'File only partially uploaded'; break; case 4: echo 'No file uploaded'; break; } exit; } // Does the file have the right MIME type? // put the file where we'd like it $upfile = '/timporn/uploads/'.$_FILES['userfile']['name']; echo $upfile; if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile)) { echo '<br>Problem: Could not move file to destination directory'; exit; } } else { echo 'Problem: Possible file upload attack. Filename: '; echo $_FILES['userfile']['name']; exit; } echo 'File uploaded successfully<br><br>'; // reformat the file contents $fp = fopen($upfile, 'r'); $contents = fread ($fp, filesize ($upfile)); fclose ($fp); $contents = strip_tags($contents); $fp = fopen($upfile, 'w'); fwrite($fp, $contents); fclose($fp); // show what was uploaded echo 'Preview of uploaded file contents:<br><hr>'; echo $contents; echo '<br><hr>'; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/222689-cannot-get-move_uploaded_file-to-work-properly/ Share on other sites More sharing options...
PHPTOM Posted December 26, 2010 Share Posted December 26, 2010 And what does the page actually say when you try to upload a file? Quote Link to comment https://forums.phpfreaks.com/topic/222689-cannot-get-move_uploaded_file-to-work-properly/#findComment-1151640 Share on other sites More sharing options...
timpickens Posted December 26, 2010 Author Share Posted December 26, 2010 Problem: Could not move file to destination directory Quote Link to comment https://forums.phpfreaks.com/topic/222689-cannot-get-move_uploaded_file-to-work-properly/#findComment-1151645 Share on other sites More sharing options...
QuickOldCar Posted December 27, 2010 Share Posted December 27, 2010 Only thing I see is this if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile)) Are doing a check for if move_uploaded_file() was done, but yet you never actually moved it with the function. Quote Link to comment https://forums.phpfreaks.com/topic/222689-cannot-get-move_uploaded_file-to-work-properly/#findComment-1151682 Share on other sites More sharing options...
PFMaBiSmAd Posted December 27, 2010 Share Posted December 27, 2010 ^^^ That if() statement calls the function and tests the result of the function call. Your most likely problem is the leading slash on your $upfile value refers to the root of the current hard disk and it is unlikely that is where your destination folder actually is. Are you developing and testing this code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php detected errors will be reported and displayed? Doing so would show any errors that are occurring with the move_uploaded_file() statement. Quote Link to comment https://forums.phpfreaks.com/topic/222689-cannot-get-move_uploaded_file-to-work-properly/#findComment-1151683 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.