How to stop a large download at startup?

I am using apache, PHP.

So, for php, even I set the maximum file size, if someone uploads a huge file, he uploads it all to the server, and then returns a PHP error. it is right?

I want the web server or PHP to stop when the file size or request size is larger than a certain size.

Is there any way to do this? I remember something like setting post_max_size or setting something in apache.conf, but not sure if it will stop when the file goes to the maximum limit.

  • I do not trust client side validation. so i'm looking for apache or php solution. :)
+7
source share
7 answers

Apache LimitRequestBody , afaik, it just stops the connection on transition ....

+4
source

It is not possible to stop this from the server side - the file will still be loaded before PHP decides whether to reject it. You will need to add this discovery on the client side. Although you cannot do this with the usual HTML form, Flash downloaders can find out the file size before starting the download.

On the server side, you cannot use anything with PHP - the files were always fully loaded before PHP could say no. To block the download (i.e. disconnect part of the client part via POST), you will need the magic of the web server below the stack. Perlbal is capable of doing this. Writing your own Apache module is a solution (brute force). Using any lightweight proxy in front of your main web server is the solution if you are worried about large downloads linking web server resources.

It really depends on why you want to limit large downloads. If this saves users time, just add a messenger that indicates the maximum file size. If these are server resources, use a lightweight proxy in front of the server. If this is something else, well, what is it?

+2
source

max_execution_time and max_input_time These settings determine the maximum service life of the script and the time that the script should spend on receiving input. If several megabytes of data are transferred, max_input_time should be high enough. You can override the ini parameter of the file for max_input_time by calling the set_time_limit () function in your scripts.

This is taken from http://www.radinks.com/upload/config.php

If you set lower values ​​for these ini entries, the script should expire before loading is complete

+2
source

Use a hidden field named MAX_FILE_SIZE with a value indicating the maximum file size in bytes. See Example No. 1 on this page for more information.

 <!-- The data encoding type, enctype, MUST be specified as below --> <form enctype="multipart/form-data" action="__URL__" method="POST"> <!-- MAX_FILE_SIZE must precede the file input field --> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <!-- Name of input element determines name in $_FILES array --> Send this file: <input name="userfile" type="file" /> <input type="submit" value="Send File" /> </form> 
+1
source

Here is a simple jquery solution How to check file input size using jQuery? But client-side validation can be easily cracked.

0
source

Use $ _FILES in php.

 if($_FILES['name']['size'] > $limit){ // continue; } 
0
source

Use a timer (timeout) for each download using the session progress function in PHP 5.5 and measure the average time per download size.

0
source

All Articles