46

I have a following problem, I have HTML form that uploads a file with some extra information. But it allows to upload files that only less then 10MB. But when user tries to upload something bigger, both $_POST and $_FILES array are empty (I expected that $_POST will have some values and $_FILES will have some values but will indicate that there is an upload error).

There is a few questions (empty $_POST, $_FILES) like that, but I didn't find any solution, or explanation for it.

HTML form:

<form enctype="multipart/form-data" method="post" action="upload.php">
    <p>
        <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
        <input type="file" name="image"  />
    </p>
    <p>
        <input type="text" name="other_field" />
    </p>
</form>

upload.php

print_r($_POST);  // array()
print_r($_FILES); // array()
exit;

It works fine, if file size is under 10MB (file size limit is 10MB), and I don't want to increase it, I just want to capture an error in PHP.

Updated (explanation/solution) from PHP site

From PHP site (I missed this section): http://us.php.net/manual/en/ini.core.php#ini.post-max-size

Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize. If memory limit is enabled by your configure script, memory_limit also affects file uploading. Generally speaking, memory_limit should be larger than post_max_size. When an integer is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used. If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty. This can be tracked in various ways, e.g. by passing the $_GET variable to the script processing the data, i.e. , and then checking if $_GET['processed'] is set.

hakre
  • 193,403
  • 52
  • 435
  • 836
Dmitry F
  • 1,650
  • 1
  • 15
  • 20

5 Answers5

48

As noted in the edited question $_POST and $_FILES are empty when PHP silently discards data (happens when the actual data is bigger than post_max_size). Since HTTP header and $_GET remain intact those can be used to detect the discards.

Option a)

if(intval($_SERVER['CONTENT_LENGTH'])>0 && count($_POST)===0){
    throw new Exception('PHP discarded POST data because of request exceeding post_max_size.');
}

Option b)
Add a GET parameter that tells whether POST data is present.

Augustus Kling
  • 3,303
  • 1
  • 22
  • 25
  • 2
    Note: $_SERVER['CONTENT_LENGTH'] is not "always" set, the server host can disable this variable from being visible – evilReiko Aug 30 '13 at 19:02
  • Thanks!!!! Finally found why an HTTP form with input of type file upload had an empty payload while uploading a file! File was too big! And your solution also put everything back to work at perfection. – Jackie Degl'Innocenti Jun 13 '20 at 09:35
16

Run phpinfo() and check to make sure your upload_max_filesize and post_max_size directives are large enough.

http://us.php.net/manual/en/ini.core.php#ini.upload-max-filesize

http://us.php.net/manual/en/ini.core.php#ini.post-max-size

WWW
  • 9,734
  • 1
  • 29
  • 33
  • 2
    This doesn't address the question of why `$_POST` is empty. If there was an error with file size, you'd expect an error message, not an empty `$_POST`. – brentonstrine Jun 10 '14 at 22:30
  • @brentonstrine Are you **seriously** necro'ing a post from October of 2011 - almost three years ago - to complain about an answer and downvote? The OP was trying to send files larger than their `post_max_size`. This, in turn (read the OP), makes `$_POST` and `$_FILES` empty. – WWW Jun 11 '14 at 13:51
  • 4
    The intent wasn't to complain--though I see how it comes across that way. Your answer was voted higher than Augustus' answer which goes much further than yours to actually explain what the issue is and even provides a solution to detect when `$_POST` is empty because it exceeded the max. I upvoted his and downvoted yours so that his answer would be first, since it's more useful. The comment was just meant as an explanation for the downvote. I stand by the downvote though: the critical piece of information is *why* `$_POST` would be empty. – brentonstrine Jun 11 '14 at 16:05
  • post_max_size was causing the issues for me. Thanks @Crontab – Jack Vial Oct 12 '16 at 20:53
1

There are some limits - both on client and server side.

On client side, the MAX_FILE_SIZE field is not of much use, perhaps browser may take it as a hint; but rather browsers follow their configured limits.

On server side, check php.ini for:

upload_max_filesize = 5M

post_max_size = 5M 

max_input_time = ...

Also check Apache's log for notes about dropped POST body or such.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
0

i can mention that if you process form like below

if(isset($_POST['submit'])

if post_max_size is lower than posted data then the $_POST['submit'] will be empty

-1

You can check for upload errors with:

if ($_FILES['images']['error'] !== UPLOAD_ERR_OK) {
   die("Upload failed with error code " . $_FILES['images']['error']);
}

The error codes are defined here.

Marc B
  • 356,200
  • 43
  • 426
  • 500