Paul15679 Posted January 14, 2009 Share Posted January 14, 2009 Hi, I'm trying to write a script to validate a user uploaded image and move it to the server, and I've encountered a problem. Whenever I tried to upload a file, the page just reloads and displays the file upload box again, regardless of whether I've tried to upload the right kind of file, or even if I have just pressed the submit button without choosing a file to upload. It's as if the PHP isn't being processed at all, and my browser is just displaying the form. My code is below, I'd be really grateful if anyone could point out where I'm going wrong. Apologies for the long post and code <?php $maxsize=28480; //set the max upload size in bytes if (!$HTTP_POST_VARS['submit']){ //print_r($HTTP_POST_FILES); $error=" "; //This will cause the rest of the processing to be skipped //and the upload form displays } if (!is_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name'])AND !isset($error)){ $error= "<b>You must upload a file!</b><br /><br />"; unlink($HTTP_POST_FILES['upload_file']['tmp_name']); } if ($HTTP_POST_FILES['upload_file']['size'] > $maxsize AND !isset($error)){ $error= "<b>Error, file must be less than $maxsize bytes.</b><br /><br />"; unlink($HTTP_POST_FILES['upload_file']['tmp_name']); } if($HTTP_POST_FILES['upload_file']['type'] != "image/gif" AND $HTTP_POST_FILES['upload_file']['type'] != "image/pjpeg" AND $HTTP_POST_FILES['upload_file']['type'] != "image/jpeg" AND !isset($error)){ $error = "<b>You may only upload .gif or .jpeg files!</b><br /><br />"; unlink($HTTP_POST_FILES['upload_file']['tmp_name']); } if (!isset($error)){ move_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name'], "uploads/".$HTTP_POST_FILES['upload_file']['name']); print "Thank you for your upload."; exit; } else{ echo ("$error"); } ?> <html> <head></head> <body> <form action="<?php echo(htmlspecialchars($_SERVER['PHP_SELF']))?>" method="post" enctype="multipart/form-data"> Choose a file to upload:<br /> <input type="file" name="upload_file" size="80"/> <br /> <input type="submit" name="submit" value="submit"/> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/140797-problem-with-php-file-upload-script/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 14, 2009 Share Posted January 14, 2009 $HTTP_POST_VARS and $HTTP_POST_FILES are turned off by default in php5 and have been completely removed in php6. Use $_POST and $_FILES Link to comment https://forums.phpfreaks.com/topic/140797-problem-with-php-file-upload-script/#findComment-736989 Share on other sites More sharing options...
Absorbator Posted January 14, 2009 Share Posted January 14, 2009 First - $HTTP_POST_VARS is deprecated, use $_POST instead Second - $HTTP_POST_FILES should be $_FILES Third - PHP interprate white space string as FALSE <?php$error=" "; //this is false ?> Link to comment https://forums.phpfreaks.com/topic/140797-problem-with-php-file-upload-script/#findComment-736991 Share on other sites More sharing options...
Paul15679 Posted January 15, 2009 Author Share Posted January 15, 2009 I changed to $_POST and $_FILES and my script works. Thanks to you both, I appreciate the help. Link to comment https://forums.phpfreaks.com/topic/140797-problem-with-php-file-upload-script/#findComment-737671 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.