Increase PHP Script Execution Time Limit
- By Preneesh AV --
- 03-Aug-2018 --
- 64 Comments
This article will show you how to increase PHP script execution time limit using ini_set() function. PHP default script execution limit is 30 seconds. Which is set in max_execution_time in php.ini file. Location of file can be obtained by running following command.
$php -i | grep "php.ini"
Possible output will be
Configuration File (php.ini) Path => /etc/php/7.0/cli
Loaded Configuration File => /etc/php/7.0/cli/php.ini
In my recent project to prevent the script from timing out, I had to set the PHP script execution time directly from with in the php file. So to execute my script more then 30 second I added below shown line at top of my file(first line).
This will set to 5 minutes time limit:
ini_set('max_execution_time',300);//300 seconds = 5 minutes |
set_time_limit() has advantage, you can call it several times and it adds the time to the total script run-time. so you can use it in a loop and it doesn’t matter how many iterations you have, you set only the time per iteration.
The function set_time_limit(seconds) does exactly the same. If seconds is set to zero, no time limit is imposed.
This will set to no time limit:
set_time_limit(0);//no limit |
If you have access to your php.ini file you can also configure execution time by changing following to something suitable to your requirement.
max_execution_time=30 |
It is important to be careful using this on a production server. If you allow a script to run too long you could use all your servers resources and your website may go down.
