0

I want to put out an error message when the uploaded image size is over 3MB. This is my current code. It puts out an error message properly when an image is over 3MB, but it also puts out an error message when no image is uploaded. How do I fix this?

//image check start
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 3072000))
//image check end
{
    if($_FILES['file']['size'] > 0)
    {
        file uploading script
    } else {
        do nothing
    }
} else {
    error("Maximum image size exceeded or invalid file format.");
}
2
  • First of all, you should check if the file is uploaded, and then check for file size and image type. Good luck! Commented Jun 12, 2012 at 1:58
  • Maybe you just need to check this answer Commented Jun 12, 2012 at 2:03

3 Answers 3

1

Well, if you consider your condition when no file is uploaded you'll see that it will fail because none of your type checks will return true, and so it'll land on the error line after the final else.

You'd need something like this:

if ( $_FILES['file']['size'] == 0 ) 
{
    // do nothing
} else {
    //image check start
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/png")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 3072000))
    //image check end
    {
        file uploading script
    } else {
        error("Maximum image size exceeded or invalid file format.");
    }
}

Obviously, if you really need to do nothing if no file is uploaded this will do:

if ( $_FILES['file']['size'] != 0 ) 
{
    //image check start
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/png")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 3072000))
    //image check end
    {
        file uploading script
    } else {
        error("Maximum image size exceeded or invalid file format.");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It's still not working but I marked your answered as accepted because now I'm using your second code. Thanks.
1

First, you should always enable error reporting in your scripts while debugging. You can do this by

error_reporting(E_ALL);
ini_set("display_errors", 1);

If you are unsure where your error is coming from, you can always try doing print_r($_FILES); and just go through the logic as you have it above. Check the $_FILES['file']['error'] variable for file upload errors. If the upload was successful it will have a value of 0.

if ($_FILES["file"]["error"] !== 0) {
    if ($_FILES["file"]["size"]) > 0) {
        if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/png")
        || ($_FILES["file"]["type"] == "image/pjpeg"))
        && ($_FILES["file"]["size"] < 3072000))
        //image check end
        {
            if($_FILES['file']['size'] > 0) {
                // file uploading script
            } 
            else {
                // do nothing
            }
        }
        else {
            error("Maximum image size exceeded or invalid file format.");
        }
    }
    else {
        error("Error uploading file. File has size of 0 bytes");
    }
}
else {
    error("There was an error uploading the file.
        File upload returned error code: " . $_FILES["file"]["error"]
    );
}

Comments

0

The problem was

upload_max_filesize = 3M 

in my php.ini file. When I changed it to

upload_max_filesize = 4M 

Everything worked just fine.

Thanks.

1 Comment

This is NOT the answer to your original question at all, and you should not be giving yourself credit. Yes, your real problem may have gone away, but others have spent time and effort answering what you actually asked, and they deserve some token respect even if you no longer care.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.