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.
The PHP
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
Place this at the top of your PHP script and let your script loose!
The <canvas>
element has been a revelation for the visual experts among our ranks. Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead. Here are nine unbelievable canvas demos that...
It's no secret that Facebook has become a major traffic driver for all types of websites. Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly. And of course there are Facebook "Like" and "Recommend" widgets on every website. One...
One of the hidden gems within the HTML5 spec is context menus. The HTML5 context menu spec allows developers to create custom context menus for given blocks within simple menu and menuitem elements. The menu information lives right within the page so...
A few weeks back, jQuery expert Janko Jovanovic dropped a sweet tutorial showing you how to create a Skype-like button using jQuery. I was impressed by Janko's article so I decided to port the effect to MooTools.
The XHTML
This is the exact code provided by...
Do with caution yo. 5 minutes execution could really hog resources.
The function set_time_limit(seconds) does exactly the same. If seconds is set to zero, no time limit is imposed.
Interesting… I never really considered the idea that there was a built-in execution limit, but that makes sense.
Do you know what the default setting in php.ini is set to?
As for Jay’s comment, that makes sense. Would sleep() hog resources still? Because it’s running, but not doing anything.
Oh nice, noticed the Gravatars. I just implemented on them my site as well.
I’d suggest running time-consuming scripts on a localhost, rather than on a live server… when possible.
A while back I had some ornery scripts that did massive statistical computations on user-generated data to produce reports. So I wrote the scripts to generate those reports and ran them on the live site where the data resided. The result was the website became unresponsive, and we’re talking about >1 minute page loads. Some of that no doubt was due to loads on the database, as well as execution of the PHP.
So I pulled a copy of the database to my local drive, and ran the scripts on localhost using that data snopshot. 5 minutes later, I got the reports I needed, then uploaded them back to the server as flat HTML files for public consumption.
Naturally this isn’t a viable option for generating reports on demand… other options may be to pre-compute using a CRON during non-peak hours (like 3AM EST) or run the scripts on another box which is capable of connecting to the live data, performing the calculations, then sending the output to the live server via FTP
This is useful for those scripts that notify search engines about new posts. Now I know how it’s done. Thanks, David!
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.
Well, I’ve got to say that tip saved my day :)
I was transferring file from another server to my server using a script and every time the script break for ( Max Execution Time )
Now I transferred all the files and reset it back to the original status!
Interesting. Thanks for this tip, or better yet, the insight.
Of late, and since the server which I have many sites had its PHP and Apache upgraded, I’ve had a number of php-apps/scripts fail from memory exceeded.
None of these installation had issues prior to the upgrade, so I imagine both the the upgrades are being a bit more strict?
BTAIM, I’ve not had much luck with either .htaccess modifications or php.ini files.
Is there a similar ini_set() modification for the above problem?
TIA,
Michael
@Michael: Should’ve tried a site search. :)
http://davidwalsh.name/increase-php-memory-limit-ini_set
Nice thing about extending the time tis way is it is for only the executing script.
hello ,
i have a doubt on file downloading. How we save that files in database,how to generate the zip code please i am waiting for your answer……
please…………………..
@rameshkaveti: Please contact me for this matter. Isn’t quite simple and depends of what are you trying to do
If you think your script will eat up all the resources, just try ini_set(“memory_limit”, “2M”) and restrict the script to only 2M of memory. On the other hand, I strongly recommend to approximate the required memory and use this for resource consuming scripts.
Great post and some helpful comments as well!
David, FYI there’s somehting on this page that’s causing it to reload in firefox or at least something is triggering the tab to read Loading… over and over again and has almost crashed my browser :/
Thanks for this great tip. It save my time :)
I have set my timeout to 3000 seconds as per your instruction…but even then my code is being executed for 10 mins and is giving me an error. :(
Is 10 minutes the maximum execution time??
I mean even if we have given anything more than that it will be executed only for 10 minutes?
1st of all, thanks for this tip, its really useful.
@Verizon Promo Code, you should first tune your coding. Reduce it by deleting unnecessary sql-queries.
@httpwebwitch, you are right ,,, but in most of the cases where we have to modify the database, we are forced to call ini_set.
Suppose I have a huge database and the coding contains so many loops, then , if we do not use ini_set, the page will not execute full, and half of my database will not modify.
So in case of reports , we can do your’s idea… but in case of modification , we can’t.
Nice tip. Here a nice explanation the other two ways to increase execution time limit (through php.ini file and set_time_limit: http://www.sudo-juice.com/php-time-limit-extend-script-running-time/
Nice tip. Here a nice explanation the other two ways to increase execution time limit (through php.ini file and set_time_limit: PHP time limit.
Hi Dear, are you truly visiting this site daily, if so after that you will without doubt take nice know-how.
i have very heavy database.so excution time is very High.
Is 10 minutes the maximum execution time??
You can use set_time_limit() for this. For a clear description visit this blog :-http://newprograminglogics.blogspot.in/2012/04/extend-maximum-execution-time-of-php.html
Short and sweet, well done, David.
No, 10 minutes is not maximum execution time. I tried it out and script ran 45+ minutes before I stopped it myself.
Just use:
set_time_limit(0);
So there is no limit.
Hi i need some help. I found 500 internal server error. for long time loading. please help me
Thanks David :)
For certain long running functions its better to set the execution time within loop, this will reset the time each time the loop executes, so you don’t have to set the time super high, but you’ll still get notified if the loop hangs at any point.
Thanks a lot, this simple function make it work.. thanks again
Thanks for the tip , after 2 hours of research, your solution made my script worked.
he thanks a lot for the ini_set tip.
what is the default execution time of php?
Default executeion time :30 secs
Could this code be added into a WordPress installation? Perhaps the wp-config file? Thx
@Fairport; yes, supposedly you can insert it into a wordpress installation.
I have tried it to debug an issue we’re having and it’s not working, though.
where exactly is this script suppose to be placed in the php.ini file?