252

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?

4
  • use set_time_limit(n) where n is the time in seconds
    – Robert
    Apr 23, 2013 at 13:55
  • 5
    This question has been downvoted and marked as duplicate yet it has a richer set of answers than said duplicate.
    – KalenGi
    Feb 28, 2014 at 7:48
  • you might need to increase the max execution time for the server, too.
    – dlicheva
    Nov 22, 2016 at 14:11
  • 1
    use ini_set('max_execution_time', 0); to get unlimited execution time. To Ignore user aborts and allow the script to run forever use ignore_user_abort(true); Apr 21, 2017 at 17:57

4 Answers 4

551
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()

7
  • works in windows but not in linux.
    – AzizAhmad
    Oct 8, 2016 at 15:13
  • 1
    Works in Linux (php-5.2.10-1.el5.centos.rpm)
    – kubanczyk
    Nov 28, 2016 at 12:28
  • 7
    This change only applies for the current running script? Or does the new maximum limit persists?
    – JCarlosR
    Dec 5, 2017 at 0:37
  • 11
    according to here: 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 5, 2017 at 7:48
  • 3
    it should be noted that the web server also has timeout directive like Apaches TimeOut this is quoted from here " 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. " May 25, 2019 at 0:34
165

use below statement if safe_mode is off

set_time_limit(0);
0
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.

1
  • 4
    I have no idea why this was downvoted as this is a perfectly formulated answer. Upvoted.
    – Fabian Pas
    Nov 25, 2013 at 15:25
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.

0

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