[php] How to increase the execution timeout in php?

How to increase transaction timeout? I want to upload videos, but large size of videos not uploaded?

It throws error The process *** exceeded the timeout of 60 seconds.

This question is related to php

The answer is


You had a typo: ini_set('max_input_time','200M') - value set needs to be an int, like ini_set('max_input_time','200')


I know you are specifically asking about the PHP timeout, but what no one else seems to have mentioned is that there can also be a timeout on the webserver and it can look very similar to the PHP timeout.

So if you have tried:

  1. Increasing the timeout in php.ini by adding a line: max_execution_time = {number of seconds i.e. 60 for one minute}
  2. Increasing the timeout in your script itself by adding: ini_set('max_execution_time','{number of seconds i.e. 60 for one minute}');

And you have checked with the phpinfo() function that max_execution_time has indeed be increased, then you might want to try adding this to .htaccess which will make sure Apache itself does not time out:

RewriteRule .* - [E=noabort:1]
RewriteRule .* - [E=noconntimeout:1]

More info here: https://www.antropy.co.uk/blog/php-script-keeps-timing-out-despite-ini-set/


optional : if you set config about php.ini but still can't upload

-this is php function to check error code

$error_type = [];
$error_type[0] = "There is no error";
$error_type[1] = "The uploaded file exceeds the upload_max_filesize directive in php.ini.";
$error_type[2] = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
$error_type[3] = "The uploaded file was only partially uploaded.";
$error_type[4] = "No file was uploaded.";
//$error_type["5"] = "";
$error_type[6] = "Missing a temporary folder. Introduced in PHP 5.0.3.";
$error_type[7] = "Failed to write file to disk. Introduced in PHP 5.1.0.";
$error_type[8] = "A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0.";
//------------------------------
//--> show msg error.
$status_code = $_FILES["uploadfile"]["error"];
if($status_code != 0){
    echo $error_type[$status_code];
    exit;
}

As an addition to above answers, you may use set_time_limit() function:

http://php.net/manual/en/function.set-time-limit.php

passing 0 as an argument will make your script run with no time limit.


If you cannot edit php.ini (on your server for example) you can attempt to change the php.ini parameters from within your php code. Try:

ini_set('max_execution_time', 'NUMBER OF SECONDS TO ALLOW BEFORE TIMEOUT');

If that doesn't work, try also setting 'set_time_limit' in the same way, beyond that I'd say your only option is to contact your host. These settings cannot be modified while in safe mode.


First check the php.ini file path by phpinfo(); and then changed PHP.INI params:

upload_max_filesize = 1000M
memory_limit = 1500M
post_max_size = 1500M
max_execution_time = 30

restarted Apache

set_time_limit(0); // safe_mode is off

ini_set('max_execution_time', 500); //500 seconds

Note: you can also use command to find php.ini in Linux

locate `php.ini`

To really increase the time limit i prefer to first get the current value. set_time_limit is not always increasing the time limit. If the current value (e.g. from php.ini or formerly set) is higher than the value used in current call of set_time_limit, it will decrease the time limit!

So what's with a small helper like this?

/**
 * @param int $seconds Time in seconds
 * @return bool
 */
function increase_time_limit(int $seconds): bool
{
    return set_time_limit(max(
        ini_get('max_execution_time'), $seconds
    ));
}

// somewhere else in your code
increase_time_limit(180);

See also: Get max_execution_time in PHP script


You should be able to do during runtime too using

set_time_limit(100);

http://php.net/manual/en/function.set-time-limit.php

or in your vhost-config

php_admin_value max_execution_time 10000

Having a global execution time limit that is LOW is mostly a good idea for performance-reasons on not-so-reliable applications. So you might want to only allow those scripts to run longer that absolutely have to.

p.s.: Dont forget about post_max_size and upload_max_filesize (like the first answer told allready)


You can also set a max execution time in your .htaccess file:

php_value max_execution_time 180

Test if you are is safe mode - if not - set the time limit (Local Value) to what you want:

if(!ini_get('safe_mode')){

    echo "safe mode off";
    set_time_limit(180);// seconds

    phpinfo();// see 'max_execution_time'
}

*You cannot set time limit this way if safe mode 'on'.


If you happen to be using Microsoft IIS server, in addition to the php.ini settings mentioned by others, you may need to increase the execution timeout settings for the PHP FastCGI application in the IIS Server Manager:

Step 1) Open the IIS Server Manager (usually under Server Manager in the Start Menu, then Tools / Internet Information Services (IIS) Manager).

Step 2) Click on the main connection (not specific to any particular domain).

Step 3) Under the IIS section, find FastCGI Settings (shown below).

enter image description here

Step 4) Therein, right-click the PHP application and select Edit....

Step 5) Check the timeouts (shown below).

enter image description here

In my case, the default timeouts here were 70 and 90 seconds; the former of which was causing a 500 Internal Server Error on PHP scripts that took longer than 70 seconds.


To complete the answer of Hannes.

You need to change some setting in your php.ini:

upload_max_filesize = 2M 
;or whatever size you want

max_execution_time = 60
; also, higher if you must

If someone want put in unlimited (I don't know why but if you want), you can set the time to 0:

You need to change some setting in your php.ini:

upload_max_filesize = 0 

max_execution_time = 0

And if you don't know where is your php.ini. You can do a file "name.php" in your server and put:

<?php phpinfo(); ?>

And on your website, you can see the config of your php.ini and it's marked where is it.

Edit on 9 January 2015:

If you can't access your php.ini, you have two more options.

You can set this line directly in your "name.php" file but I don't find for upload_max_filesize for this option:

set_time_limit(0);

Or in ".htaccess"

php_value upload_max_filesize 0
php_value max_execution_time 0

if what you need to do is specific only for 1 or 2 pages i suggest to use set_time_limit so it did not affect the whole application.

set_time_limit(some_values);

but ofcourse these 2 values (post_max_size & upload_max_filesize) are subject to investigate.

you either can set it via ini_set function

ini_set('post_max_size','20M');
ini_set('upload_max_filesize','2M');

or directly in php.ini file like response above by Hannes, or even set it iin .htaccess like below

php_value upload_max_filesize 2M
php_value post_max_size 20M