Jump to content

Problem with PHP file upload script


Paul15679

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.