aebstract Posted September 2, 2008 Share Posted September 2, 2008 Trying to get my first upload form running and running in to a little bit of trouble ??? <?php if (isset($_POST['submit'])) { if (move_uploaded_file ($_FILES['thefile']['tmp_name'], "/temp/{$_FILES['thefile']['name']}")) { echo "it worked"; } else { echo "it didn't work: <br /><br />"; switch ($_FILES['thefile']['error']) { case 1: echo "upload_max_filesize"; break; case 2: echo "MAX_FILE_SIZE"; break; case 3: echo "partially uploaded"; break; case 4: echo "no upload at all"; break; } } } echo " <form action=\"upload.php\" enctype=\"multipart/form-date\" method=\"post\"> <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"30000\" /> <input type=\"file\" name=\"thefile\" /> <input type=\"submit\" name=\"submit\" value=\"Upload\" /> </form> "; ?> When I try to upload a file, all that gets echoed is: it didn't work So I am not sure what to do really Thanks for any help in advance Quote Link to comment https://forums.phpfreaks.com/topic/122378-solved-problem-with-very-simple-upload-form/ Share on other sites More sharing options...
stevesimo Posted September 2, 2008 Share Posted September 2, 2008 Have you set the chmod of your upload folder to 777 as I had this problem once when uploading images. Quote Link to comment https://forums.phpfreaks.com/topic/122378-solved-problem-with-very-simple-upload-form/#findComment-631873 Share on other sites More sharing options...
stevesimo Posted September 2, 2008 Share Posted September 2, 2008 also enctype should be multipart/form-data Quote Link to comment https://forums.phpfreaks.com/topic/122378-solved-problem-with-very-simple-upload-form/#findComment-631879 Share on other sites More sharing options...
aebstract Posted September 2, 2008 Author Share Posted September 2, 2008 Okay the form-data was the first problem, having date was a simple typo. Now I am getting error message for case 2 back. I changed my hidden field to 300000 instead of 30000, and still get the error. Is this number the problem or something else? File size is like 1.04 MB edit: I tried a file that was like 124KB and it just returns a "it didn't work" with no case error. Quote Link to comment https://forums.phpfreaks.com/topic/122378-solved-problem-with-very-simple-upload-form/#findComment-631904 Share on other sites More sharing options...
BlueSkyIS Posted September 2, 2008 Share Posted September 2, 2008 Now I am getting error message for case 2 back what is the error message? Quote Link to comment https://forums.phpfreaks.com/topic/122378-solved-problem-with-very-simple-upload-form/#findComment-631922 Share on other sites More sharing options...
BlueSkyIS Posted September 2, 2008 Share Posted September 2, 2008 chances are your error is here: move_uploaded_file ($_FILES['thefile']['tmp_name'], "/temp/{$_FILES['thefile']['name']}")) are you sure you can write to /temp? Quote Link to comment https://forums.phpfreaks.com/topic/122378-solved-problem-with-very-simple-upload-form/#findComment-631924 Share on other sites More sharing options...
PFMaBiSmAd Posted September 2, 2008 Share Posted September 2, 2008 The value is in bytes only. It is not possible to use K, M, or G - The MAX_FILE_SIZE hidden field (measured in bytes) must precede the file input field, and its value is the maximum filesize accepted by PHP. Fooling this setting on the browser side is quite easy, so never rely on files with a greater size being blocked by this feature. The PHP settings for maximum-size, however, cannot be fooled. This form element should always be used as it saves users the trouble of waiting for a big file being transferred only to find that it was too big and the transfer failed. Quote Link to comment https://forums.phpfreaks.com/topic/122378-solved-problem-with-very-simple-upload-form/#findComment-631925 Share on other sites More sharing options...
aebstract Posted September 2, 2008 Author Share Posted September 2, 2008 My file upload.php is in my main html directory, also in my main html directory is a folder "temp". I set it to 777 so that it was fully writable. When I say case 2, in my code case 2 goes to the MAX_FILE_SIZE, which is easily fixable, I'll just set that higher. Though now that I don't get that error, I'm just getting "it didn't work: " and no case error. I'm not sure why? Quote Link to comment https://forums.phpfreaks.com/topic/122378-solved-problem-with-very-simple-upload-form/#findComment-631931 Share on other sites More sharing options...
PFMaBiSmAd Posted September 2, 2008 Share Posted September 2, 2008 You need to check for upload errors first before you attempt to do a move_uploaded_file() and you need to check for all possible error values or you need a default case statement that at least tells you what error number occurred. Add the following two lines after your <?php tag to see any php errors (such as the /temp problem that BlueSkyIS suggested) - ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/122378-solved-problem-with-very-simple-upload-form/#findComment-631938 Share on other sites More sharing options...
aebstract Posted September 2, 2008 Author Share Posted September 2, 2008 Okay, got that in: Warning: move_uploaded_file(/temp/car.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/virtual/site21/fst/var/www/html/upload.php on line 9 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpxkGAWr' to '/temp/car.jpg' in /home/virtual/site21/fst/var/www/html/upload.php on line 9 Is it saying my file name is wrong or that the directory doesn't exist? The route to my html directory is /var/www/html/ and there is a folder called temp in it. Should it be: if (move_uploaded_file ($_FILES['thefile']['tmp_name'], "/temp/{$_FILES['thefile']['name']}")) { changed to: if (move_uploaded_file ($_FILES['thefile']['tmp_name'], "/home/virtual/site21/fst/var/www/html/temp/{$_FILES['thefile']['name']}")) { edit: made the change that I just asked about above and it fixed the problem, image uploaded with ease Thanks for the troubleshooting help! Quote Link to comment https://forums.phpfreaks.com/topic/122378-solved-problem-with-very-simple-upload-form/#findComment-631950 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.