Today I came across an issue where when uploading large files my $_FILES and $_POST values returned empty arrays. Turns out this is expected (albeit stupid IMO) behavior.
You can test this by uploading a small then a huge file to this relatively simple PHP script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <!DOCTYPE html> <html> <head> <title>Upload test</title> </head> <body> <form action="?submit=true" role="form" method="POST" enctype="multipart/form-data"> <input name="images" type="file"> <input type="hidden" name="foo" value="bar"> <input type="submit"> </form> <pre>$_FILES: <?php print_r($_FILES); ?> </pre> <pre>$_POST: <?php print_r($_POST); ?> </pre> </body> </html> |
After uploading a small file your $_FILES variable will contain the file details but with a huge file (one larger than your POST_MAX_SIZE ini setting) it will be empty.
To check if a user has uploaded a file larger than your POST_MAX_SIZE value you need to use the following if statement courtesy of stackoverflow:
1 2 | if ( !empty($_SERVER['CONTENT_LENGTH']) && empty($_FILES) && empty($_POST) ) echo 'The uploaded zip was too large. You must upload a file smaller than ' . ini_get("upload_max_filesize"); |
Here is an updated file that will notify the user if the file they uploaded is too large:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <!DOCTYPE html> <html> <head> <title>Upload test</title> </head> <body> <form action="?submit=true" role="form" method="POST" enctype="multipart/form-data"> <input name="images" type="file"> <input type="hidden" name="foo" value="bar"> <input type="submit"> </form> <?php if ( !empty($_SERVER['CONTENT_LENGTH']) && empty($_FILES) && empty($_POST) ) echo 'The uploaded zip was too large. You must upload a file smaller than ' . ini_get("upload_max_filesize"); ?> <pre>$_FILES: <?php print_r($_FILES); ?> </pre> <pre>$_POST: <?php print_r($_POST); ?> </pre> </body> </html> |