31

I'm looking into what is the best value to set for defaults in PHP. I've seen many contradicting points about max_input_time.

This answer says that he believes file uploading is not counted towards timers: https://stackoverflow.com/a/3758522/518169

While on the official PHP documentation, there is a huge red warning saying:

max_input_time sets the maximum time, in seconds, the script is allowed to receive input; this includes file uploads. For large or multiple files, or users on slower connections, the default of 60 seconds may be exceeded

Source: http://php.net/manual/en/features.file-upload.common-pitfalls.php, last updated: Fri, 06 Jul 2012

So from this it seems to max_input_time does affect file uploading and to be sure that visitors can upload say 20 MB files even from slow or mobile connections, the default value of 60 is definitely not enough!

What do you recommend setting this value to? 300?

Also, is there any relationship between max_execution_time and max_input_time? For example like that max_execution_time needs to be bigger than max_input_time?

2
  • I had some problems with upload of big files and Apache timeout, but with PHP not. Commented Jul 8, 2012 at 22:10
  • Interesting enough I am having the same issues for quite some time on one of my hosting providers and although everything in the PHP configuration seems legit large uploads on slower connections result in HTTP/1.1 500 Internal Server Error. Now that I have seen that quote in the first answer I am beginning to wonder whats the real deal. Will be keeping a close eye on this thread and try to dig something up by myself.
    – user188654
    Commented Jul 9, 2012 at 13:50

4 Answers 4

21

After some quick benchmarking I do not believe max_input_time has any bearing on handling large uploads by users with slow connections.

From https://www.php.net/manual/en/info.configuration.php#ini.max-input-time

This sets the maximum time in seconds a script is allowed to parse input data, like POST and GET. It is measured from the moment of receiving all data on the server to the start of script execution.

I'm using PHP 5.3.8 and used the following .htaccess config

php_value max_input_time 5
php_value max_execution_time 1
php_value upload_max_filesize "2048M"
php_value post_max_size "2048M"

My test script is:

<?php
if (!empty($_FILES)) {
    echo '<pre>';
    var_dump($_FILES);
    echo '</pre>';
}
?>
<form enctype="multipart/form-data" method="POST">
    File: <input name="userfile" type="file" />
    <input type="submit" value="Upload" />
</form>

With several trials my 1.5G file takes around 16-17 seconds to upload, 4-5 seconds to process, and execution time is essentially 0.

With max_input_time 5 the script completes. With it set to 4 we get PHP Fatal error: Maximum execution time of 4 seconds exceeded in Unknown on line 0, referer: http://localhost/test-upload.php

It also seems max_execution_time has no bearing since we kept it at 1 throughout the tests.

6
  • 6
    Just wanna say, this answer is incorrect. In rare cases, the slow connection will get you Broken pipe error. I've just fixed it today, and it is to set up the max_input_time to 600. and I am googling now how to calculate the max_input_time, but only see the incorrect answer. Commented Jul 20, 2014 at 11:33
  • @BenP.P.Tung, This is important. Please add more details.
    – Pacerier
    Commented Mar 25, 2015 at 14:37
  • 1
    This answer is ENTIRELY incorrect. Perhaps it was changed in PHP 5.4? Anyway, I can confirm 100% that max_input_time will cause a 500 error if uploading the file takes longer than max_input_time. I debugged this issue for hours and finally changed this one value and it was fixed. Tested on PHP 5.4.16 and IIS 7.5. Not only that, but the PHP manual now states "This sets the maximum time in seconds a script is allowed to parse input data, like POST and GET. Timing begins at the moment PHP is invoked at the server and ends when execution begins," which includes upload time. goo.gl/FTfsD3
    – dallin
    Commented Jan 14, 2016 at 16:54
  • I'll add that the error logs all said max_execution time had been reached rather than max_input_time for some reason, even when I set max_execution_time to 10000.
    – dallin
    Commented Jan 14, 2016 at 17:02
  • I have both max_input_time and max_execution_time currently set to 2. Neither seems to affect my file uploading that I do through XMLHttpRequest Level 2 - it lasts for several minutes already. I'm on PHP 5.6.6. Perhaps it is a web server itself that contributes to broken pipe error. At least I know for sure that there can be set timeouts for requests.
    – jayarjo
    Commented Jul 25, 2016 at 15:07
7

I did extensive study on max_input_time. Network transfer time is not a factor. PHP as Apache handler (mod_php) or Nginx/PHP-FPM -pair yielded similar results: PHP gets the uploaded file once the transfer is completed and web server hands the data over. On my tests 2 second max_input_time was enough to handle a 800 MiB upload.

All the details are at http://blog.hqcodeshop.fi/archives/185-PHP-large-file-uploads.html

5

It's going to depend on how the PHP is bridged to the webserver.

Technically it's possible for the webserver to invoke PHP as soon as it has the request headers - in which case PHP is going to be twiddling it's thumbs waiting for the POST data to come across the internet until it can populate the request variables (it's quite possible that max_input_time will be exceeded). But more commonly, the webserver will delay the invocation of PHP until it has the the full request (it's a lot less likely that max_input_time wil be exceeded).

3
  • 2
    Care to provide any further read on this topic? This is the first time I came upon a claim that PHP processing is invoked right at the beginning of the request thus affecting max_input_time. Thanks.
    – user188654
    Commented Jul 9, 2012 at 13:54
  • @symcbean, Which implementation does that? Doesn't sound like a good way to layer apps since the PHP script would now need to bother itself with things like connection broken halfway and etc.
    – Pacerier
    Commented Feb 2, 2015 at 12:51
  • I can confirm that that this does happen. I was getting a 500 error on large files that took over 60 seconds to upload. I changed only max_input_time and it fixed it. Not only that, the php manual now has different wording than quoted above. It now says timing starts "at the moment PHP is invoked at the server", instead of "from the moment of receiving all data on the server." You can read it here: goo.gl/FTfsD3. The weird thing is the error in the PHP logs said max_execution_time had been reached, even when I set it to 10000.
    – dallin
    Commented Jan 14, 2016 at 17:01
2

As of PHP 5.4, PHP file uploads can definitely be affected by max_input_time. I recently was getting a 500 error on files that took longer than 60 seconds to upload. I changed this single value in my php.ini and it went away.

In addition, the wording in the manual is different now from what is quoted in the accepted answer. It now says:

This sets the maximum time in seconds a script is allowed to parse input data, like POST and GET. Timing begins at the moment PHP is invoked at the server and ends when execution begins.

I was using PHP 5.4.16 nts and IIS 7.5. Apparently, PHP is invoked before the file is uploaded.

One interesting thing to note is my PHP error logs gave the wrong error. They stated "PHP Fatal error: Maximum execution time of 10000 seconds exceeded in...". It didn't matter what I set max_execution_time to, it would give the same error with the new number.

Not the answer you're looking for? Browse other questions tagged or ask your own question.