Increase PHP Script Execution Time Limit Using ini_set()

Every once in a while I need to process a HUGE file. Though PHP probably isn't the most efficient way of processing the file, I'll usually use PHP because it makes coding the processing script much faster. To prevent the script from timing out, I need to increase the execution time of the specific processing script. Here's how I do it.

php code

ini_set('max_execution_time', 300); //300 seconds = 5 minutes

Place this at the top of your PHP script and let your script loose!

PHP time limit – extend script running time with php.ini

Some scripts require some time to execute, and they might reach your PHP time limit.

Fatal error

If PHP time limit is too short for your script, you will see this error:

Fatal error: Maximum execution time of N seconds exceeded in /path/to/script.php on line N

Configuring php.ini

PHP time limit can be configured in php.ini, open it up and change the following line to something more suitable:

(EDIT: Can’t find it? Look where is php.ini)

max_execution_time = 30

NOTE: PHP time limit is measured in seconds!

In order to enable PHP time limit, Apache needs to be restarted after php.ini has been edited.

Setting PHP time limit within a script

If you can’t edit your php.ini, or you want to set a time limit only for one script, you can set it with set_time_limit(N) function, which sets the time limit in seconds.

This will set to no time limit:


And this line to one minute:


NOTE: When dealing with resource-consuming operations, PHP might exceed the memory limit! However, other than PHP time limit, you can also set the PHP memory limit.


Visit sunny St. George, Utah, USA