Skip to main content

Increasing the PHP script execution time


How to Increase the PHP script execution time?

I am using a csv processing code , when i tried to import the csv content to the table using the php script, it take too much time and after that a download pop of the executing scripts name is showing...I think this is due to execution time problem. How to fix this?



Three ways we can solve this. : Scroll down for the solution.



Three ways we can solve this.

1) set_time_limit() function

usage : void set_time_limit ( int $seconds )

Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.

When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.

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.

code to check safe mode and try the function:
<?php
if( !ini_get('safe_mode') ){
set_time_limit(50);
}
?>


set_time_limit has another advantage, you can call it several times and it adds the time to the total script runtime. 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.

########### ###########

2) ini_set() function

string ini_set( string $varname , string $newvalue )

The php function ini_set sets the value of the given configuration option. The configuration option will keep this new value during the script's execution, and will be restored at the script's ending.

Usage ini_set( "max_execution_time" , "55" )

########### ###########

3) Modifying `max_execution_time' value in PHP configuration(php.ini) file

This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 30. When running PHP from the command line the default setting is 0.



options 1 and 2 are using for temporarily purpose and will persist until the scripts execution completed ie, The configuration option will keep this new value during the script's execution, and will be restored at the script's ending. Option 3 is for permanent purpose.


More info:
http://php.net/manual/en/function.set-time-limit.php
http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time

Comments

Popular posts from this blog

Strange problem occured while trying to create a CSV file using PHP Script - The file is not seen on FTP but can download using file's absolute path url

Strange problem occured while trying to create a CSV file - The file is not seen on FTP but can download using file's absolute path url Last day I came across a strange problem when I tried to create a csv file on therver using a PHP script. the script was simply writing a given content as a csv file. The file will be created runtime. What happened was, The script executed fine, file handler for new file was created and contents was wrote into the file using fwrite and it returned the number of bytes that was written.

How to get the Query string of a URL using the Javascript (JS)?

JS function get the Query string of a URL or value of each parameter using the Javascript(JS)? If you want to get your current page's url var my_url=document.location; to get the query string part of the url use like this: var my_qry_str= location.search; this will return the part of the url starting from "?" following by query string Lets assume that your current page url is http://www.crozoom.com/2013/page.html?qry1=A&qry2=B then the location.search function will return " ?qry1=A&qry2=B " to exclue "?", do like this:


Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »
Member
Search This Blog