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. Place this at the top of your PHP script and let your script loose! Some scripts require some time to execute, and they might reach your PHP time limit. 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 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] 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.php code
ini_set['max_execution_time', 300]; //300 seconds = 5 minutes
PHP time limit – extend script running
time with php.ini
Fatal error
Configuring php.ini
max_execution_time = 30
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:
set_time_limit[0];
And this line to one minute:
set_time_limit[60];
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.
How to increase or decrease the maximum execution time of a php scriptBy default PHP script execution time is set for 30 seconds at php.ini file. This is the time limit set for all the files to finish its execution. If the file takes more than this set time then the execution of the script will be stopped and error message will be displayed like this. This maximum execution time limit is set inside the php.ini file like this. This value can be increased or decreased as per our requirement at php.ini file but note that in a server environment where many sites are hosted, any change in php.ini will affect all the sites hosted by the server. So this change is not possible unless you own the server and you are the only user. In a shared hosting our host may not like to change any settings of php.ini for our
requirement. So there is a option to set the file execution time for the particular file at the individual file end. This adjustment of time limit has no effect if php is running in safe mode. Let us try to learn how this works. We will use time stamp to find out the delay between starting and ending of a script execution. We will store the
time stamp value at the starting of the script and then again we will record at the end of the script . The difference of these two values will give us idea how much time the script has taken to execute. Note that here between two values of time we will try to add some delay by using sleep[] function. This sleep function takes numeric
value in seconds and delay the execution for that many seconds. We can also create delay by using for loop but this is a better solution. Here is the code. We have introduced a delay of 20 seconds and this will execute without any problem, now increase the sleep value to 50 seconds and on execution you will get the error message saying maximum execution time exceeded. This is because by default the
maximum execution time set at php.ini is 30 seconds. Now let us change the above code by adding one line set_time_limit[60]. Here is the code Now the script will execute fine without any error message. We can see the total time taken by the script. We can set the time to unlimited value by making it to 0 value. Like this
Subscribe* indicates required Email Address * First Name Last Name Subscribe to plus2net | ||||
| ||||
Post your comments , suggestion , error , requirements etc here . Detail |