views:

197

answers:

3

I am working on a PHP upload script and when testing my error checks, I attempted to upload a 17MB TIFF file. When I do this the $_FILES array is empty. The script works fine for what I need it to do, which is to upload JPEG files. My solution is to test if $_FILES is empty or not before continuing with the upload script.

Can anybody explain why $_FILES is empty when a TIFF is attempted to be uploaded? Is my solution, to check if $_FILES is empty or not, an okay one?

Does this have something to do with settings in php.ini?

Just to clarify

I was checking that $_FILES was empty using the following:

if(empty($_FILES))
{
    die('$_FILES is empty.');
}
+1  A: 

You should check to see if $_FILES['file']['error'] is equal to 0. This indicates a "success".

If your files array is empty, it might be due to some other problem, like not including the enctype.

Try doing var_dump($_FILES) and viewing the contents...

EDIT: I know you can set the max filesize in the php.ini file, but I am not sure if that will give you an empty files array. I think you will just get an error.

webdestroya
Good advice. To var_dump an empty array
Col. Shrapnel
@Col - Since he sounds like a beginner, it was to help verify the array is indeed empty, and that he isnt viewing the wrong index.
webdestroya
+6  A: 

Yes, upload_max_filesize controls max upload size, which the TIFF file most likely exceeded. The default is 2M. You can test with:

echo ini_get("upload_max_filesize");

EDIT: Actually, the exact cause is more likely post_max_size, which is always >= upload_max_filesize: "If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty."

Matthew Flaschen
@Matthew Thanks! It was definitely because the TIFF file size exceeded both `upload_max_filesize` and `post_max_size`.
letseatfood
A: 

As posted already, it's empty because if failed. Also check for a form element like this:

<input type="hidden" name="MAX_FILE_SIZE" value="-maxsizegoeshereinbytes-" />

To be extra sure (once you actually have an array - adding to the 'check the error key' post, btw) , you can also check the size

if( $_FILES['file']['size'] > 0 ) {

}
Dan Heberden
That's not true. Only PHP pays attention to the MAX_FILE_SIZE field.
Matthew Flaschen
Updated answer - thanks for that detail. Though still applicable, as this is php.
Dan Heberden
Your answer is still irrelevant
Col. Shrapnel
How? If i try to upload a 40 MB file into a form with <input type="hidden" name="MAX_FILE_SIZE" value="10000" />, even if php has a limit of 100MB, it will still result in an empty $_FILES array.
Dan Heberden
@Col. Shrapnel. Instead of just stating that something is irrelevant, please explain why.
letseatfood
OMG @Mike you've already got your answer, the other one.
Col. Shrapnel
@Col. Shrapnel Super helpful thanks!!! Omg.
letseatfood