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

01-08-2008, 03:28 AM

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.

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 />";
if ($maxsize < $_FILES['upload_file']['size'] AND !isset($error)) {
$error = "<b>Error, file must be less than $maxsize bytes.</b><br /><br />";
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 />";
if (!isset($error)) {
print "Thank you for your upload.";
echo ("$error");

<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">

01-08-2008, 07: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