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.
The filesize() function in PHP is an inbuilt function which is used to return the size of a specified file. The filesize() function accepts the filename as a parameter and returns the size of a file in bytes on success and False on failure.
By default, PHP permits a maximum file upload of 2MB. You can ask users to resize their images before uploading but let's face it: they won't. Fortunately, we can increase the limit when necessary. Two PHP configuration options control the maximum upload size: upload_max_filesize and post_max_size .
The move_uploaded_file() function moves an uploaded file to a new destination. Note: This function only works on files uploaded via PHP's HTTP POST upload mechanism. Note: If the destination file already exists, it will be overwritten.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With