247

I want to increase maximum execution time in php , not by changing php.ini file.

I want to Increase it from my php file.

Is this possible?

azro
  • 53,056
  • 7
  • 34
  • 70
Pritesh Mahajan
  • 4,974
  • 8
  • 39
  • 64

4 Answers4

543
ini_set('max_execution_time', '300'); //300 seconds = 5 minutes
ini_set('max_execution_time', '0'); // for infinite time of execution 

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

Taken from Increase PHP Script Execution Time Limit Using ini_set()

Martin.
  • 10,494
  • 3
  • 42
  • 68
James Scholes
  • 7,686
  • 3
  • 19
  • 20
  • works in windows but not in linux. – AzizAhmad Oct 08 '16 at 15:13
  • 1
    Works in Linux (php-5.2.10-1.el5.centos.rpm) – kubanczyk Nov 28 '16 at 12:28
  • On PHP 7.0.22, when I set the time limit with `ini_set('max_execution_time', '7200');`, I don't understand why the script still runs at most 120 seconds and I get the error `Fatal error: Maximum execution time of 120 seconds exceeded in`. Any idea? – tonix Nov 27 '17 at 10:08
  • 7
    This change only applies for the current running script? Or does the new maximum limit persists? – JCarlosR Dec 05 '17 at 00:37
  • 11
    according to here: http://php.net/manual/en/function.ini-set.php, "The configuration option will keep this new value during the script's execution, and will be restored at the script's ending" – Va1iant Dec 05 '17 at 07:48
  • 3
    it should be noted that the web server also has timeout directive like Apaches [TimeOut](http://httpd.apache.org/docs/2.0/mod/core.html#timeout) this is quoted from [here](https://www.php.net/manual/en/info.configuration.php#ini.max-execution-time) **" Your web server can have other timeout configurations that may also interrupt PHP execution. Apache has a Timeout directive and IIS has a CGI timeout function. Both default to 300 seconds. See your web server documentation for specific details. "** – Accountant م May 25 '19 at 00:34
  • What if time is setted like this: `ini_set('max_execution_time', '-1');` Is this similar to 0, that will be a infinite tiem of execution? – Álvaro Mar 03 '23 at 09:15
165

use below statement if safe_mode is off

set_time_limit(0);
Amir
  • 4,089
  • 4
  • 16
  • 28
67

Use the PHP function

void set_time_limit ( int $seconds )

The maximum execution time, in seconds. If set to zero, no time limit is imposed.

This function has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the php.ini.

TheEwook
  • 11,037
  • 6
  • 36
  • 55
35

You can try to set_time_limit(n). However, if your PHP setup is running in safe mode, you can only change it from the php.ini file.

Adam Wright
  • 48,938
  • 12
  • 131
  • 152