How to determine if a user uploaded a file more than post_max_size?

How do I handle HTTP download processing that is superior to post_max_size reasonable way?

In my configuration, post_max_size a few MB more than upload_max_filesize I have problems:
If the user uploads a file exceeding post_max_size

  • Array _POST is empty
  • The _FILES array is empty, and, of course, there are no error codes in it.
  • No other information about what form of communication is available is available through these points.

Part of the problem is that the receiving script takes different actions depending on the contents of the POST.

I have access to _SERVER variables and you can find out what happened, i.e. CONTENT_TYPE , CONTENT_LENGTH and REQUEST_METHOD . However, it seems very problematic to make guesses based on this data.

MEMORY_LIMIT (installed 10 times the corresponding size) and Apache LimitRequestBody (set to an unlimited number), as installed, will not be to blame.

Currently, it’s not easy for me to even provide any meaningful messages to the user.

Is there a way to save some form data in order to better understand what went wrong? I was very reluctant to step away from php.

+8
php file-upload
source share
6 answers

For a simple fix that does not require changes on the server side, I would use the HTML5 file API to check the file size before downloading. If it exceeds a known limit, cancel the download. I believe something like this will work:

 function on_submit() { if (document.getElementById("upload").files[0].size > 666) { alert("File is too big."); return false; } return true; } <form onsubmit="return on_submit()"> <input id="upload" type="file" /> </form> 

Obviously, this is just an example skeleton, and not every browser supports this. But this does not hurt to use it, because it can be implemented in such a way that it does not elegantly turn into nothing for older browsers.

Of course, this does not solve the problem, but at least it will support a number of your users with minimal effort. (And they don’t even have to wait until the download is complete.)

-

Aside, checking $_SERVER['CONTENT_LENGTH'] and the size of the message and file data may help to find that something failed. I think that when there is an error, it will not be zero, and the tags $_POST and $_FILES will be empty.

+8
source share

In the PHP documentation :

If the post data is larger than post_max_size, the superglobal $ _POST and $ _FILES are empty. This can be tracked in various ways, for example. passing the variable $ _GET to the script data processing, i.e. <form action = "edit.php? processed = 1">, and then check if $ _GET ['processed'] is set.

If you need to increase the limit for a specific script, you can try ini_set ('post-max-size', $ size_needed) ;. I'm not sure if it can be overridden in a script; this limit probably exists to prevent you from doing what you are trying to do.

+3
source share

I liked @Matthew's answer, but I need a version that checked multiple downloads.

This was my solution:

 function checkAttachmentsSize() { var total = 0; var count = 0; jQuery('input[type="file"]').each( function() { if (typeof this.files[0] != 'undefined') { total+= this.files[0].size; count++; } } ); var word = (count > 1) ? are' : ' is'; if (total > (uploadMax * 1000 * 1000)) { alert("The attachment file" + word + " too large to upload."); return false; } return true; } 

And for completeness, here is the function binding to the presented form:

 jQuery(function($) { $("form").submit( function() { return checkAttachmentsSize(); } }); ); 

Note:
uploadMax is a variable that I set via php after calculating the maximum size of a valid upload.

+1
source share

You can solve this problem on the server side without resorting to the query string. Just compare the setting * post_max_size * with the expected length of the request content. The following code is how Cohan does it.

 public static function post_max_size_exceeded() { // Make sure the request method is POST if (Request::$initial->method() !== HTTP_Request::POST) return FALSE; // Get the post_max_size in bytes $max_bytes = Num::bytes(ini_get('post_max_size')); // Error occurred if method is POST, and content length is too long return (Arr::get($_SERVER, 'CONTENT_LENGTH') > $max_bytes); } 
+1
source share

You may need to revert to something that uses flash / silverlight, etc., for example: http://www.plupload.com/

Or look at the Java solution ...

Basically something that will break the load into more manageable (and renewable) chunks and then collect them on the server side again.

0
source share

If your upload form has fields other than the file input field, then $ _POST should be empty: files are processed exclusively through $ _FILES. It is very strange that $ _FILES will be empty if the message size is exceeded - load handlers have a certain error code (1 / UPLOAD_ERR_INI_SIZE ) to report such a condition. Also check that memory_limit larger than upload_max_filesize.

Your web server may also block the download that will occur before PHP is called. On Apache, it is controlled by LimitRequestBody .

0
source share

All Articles