...

View Full Version : Upload - can't get it to work



mamoosh
01-08-2008, 02:28 AM
Hi!

Can someone help me with this script. I went over it 100 times, but I can't seem to find what's wrong with it.

Everytime I press Submit, the page refreshes, and nothing gets uploaded.



<?php
$maxsize=2621440;
if (!$HTTP_POST_VARS['submit']) {
$error=" ";

}
if (!is_uploaded_file($_FILES['upload_file']['tmp_name']) AND !isset($error)) {
$error = "<b>You must upload a file!</b><br /><br />";
unlink($_FILES['upload_file']['tmp_name']);
}
if ($maxsize < $_FILES['upload_file']['size'] AND !isset($error)) {
$error = "<b>Error, file must be less than $maxsize bytes.</b><br /><br />";
unlink($_FILES['upload_file']['tmp_name']);
}
if($_FILES['upload_file']['type'] != "image/gif" AND
$_FILES['upload_file']['type'] != "image/png" AND
$_FILES['upload_file']['type'] != "image/jpg" AND
$_FILES['upload_file']['type'] !="image/jpeg" AND !isset($error)) {
$error = "<b>You may only upload .gif, .png, or .jpeg files.</b><br /><br />";
unlink($_FILES['upload_file']['tmp_name']);
}
if (!isset($error)) {
move_uploaded_file($_FILES['upload_file']['tmp_name'],"uploads/".$_FILES['upload_file']['name']);
print "Thank you for your upload.";
exit;
}
else
{
echo ("$error");
}
?>

<html>
<head></head>
<body>
<form enctype="multipart/form-data" action="<?php echo( htmlspecialchars($_SERVER['PHP_SELF'])) ?>" method="POST">
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>

CFMaBiSmAd
01-08-2008, 06:27 AM
$HTTP_POST_VARS are depreciated and are off by default. Check your web server log for errors or turn on full php error reporting to get php to help you out. If they are off, then your code sets $error = " "; and all the rest of the code skips their checks. Your code then echos a " " and displays the form again.

Your code also does not bother to check the ['error'] element for any upload errors before it starts checking the other elements, such as ['tmp_name'], ['size'], ... So, anytime you get a error during the upload, the other tests will fail and give you an error that does not really have anything to do with what the real problem was - http://www.php.net/manual/en/features.file-upload.errors.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum