O'Reilly

Increase PHP Script Execution Time Limit Using ini_set()

By on  

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!

O'Reilly Velocity Conference
Save 20% with discount code AFF20

Recent Features

  • Facebook Open Graph META Tags

    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...

  • Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event,...

Incredible Demos

  • Facebook Sliders With Mootools and CSS

    One of the great parts of being a developer that uses Facebook is that I can get some great ideas for progressive website enhancement. Facebook incorporates many advanced JavaScript and AJAX features: photo loads by left and right arrow, dropdown menus, modal windows, and...

  • Fancy FAQs with MooTools Sliders: Version 2

    A little over a year ago I authored a post titled Fancy FAQs with MooTools Sliders. My post detailed a method of taking boring FAQs and making them more robust using the world's best JavaScript framework: MooTools. I've taken some time to...

Discussion

  1. Jay

    Do with caution yo. 5 minutes execution could really hog resources.

  2. The function set_time_limit(seconds) does exactly the same. If seconds is set to zero, no time limit is imposed.

  3. 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.

  4. Oh nice, noticed the Gravatars. I just implemented on them my site as well.

  5. 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

  6. This is useful for those scripts that notify search engines about new posts. Now I know how it’s done. Thanks, David!

  7. 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.

  8. 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!

  9. 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

  10. @Michael: Should’ve tried a site search. :)

    http://davidwalsh.name/increase-php-memory-limit-ini_set

  11. Nice thing about extending the time tis way is it is for only the executing script.

  12. rameshkaveti

    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…………………..

  13. @rameshkaveti: Please contact me for this matter. Isn’t quite simple and depends of what are you trying to do

  14. 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.

  15. 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 :/

  16. James

    Thanks for this great tip. It save my time :)

  17. 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?

  18. 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.

  19. uboonto

    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/

  20. uboonto

    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.

  21. Hi Dear, are you truly visiting this site daily, if so after that you will without doubt take nice know-how.

  22. i have very heavy database.so excution time is very High.

    Is 10 minutes the maximum execution time??

  23. 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

  24. Iliyan Petrov

    Short and sweet, well done, David.

  25. Anon

    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.

  26. Nathan

    Hi i need some help. I found 500 internal server error. for long time loading. please help me

  27. Thanks David :)

  28. Randy

    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.

  29. Thanks a lot, this simple function make it work.. thanks again

  30. Thanks for the tip , after 2 hours of research, your solution made my script worked.
    he thanks a lot for the ini_set tip.

  31. linda

    what is the default execution time of php?

  32. kaspar

    Default executeion time :30 secs

  33. Fairport

    Could this code be added into a WordPress installation? Perhaps the wp-config file? Thx

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • Prevent Chrome from Translating a Page

    A while back I shared my favorite Google Chrome extension:  Google Art Project.  I've enjoyed seeing beautiful art when I open a new tab -- it's brought genuine happiness to my day, however small that happiness may be.  About a week ago, however, the art presented had...

  • Create Any Type Of Website With These Multi-Purpose Themes

    We have selected what we believe are the very best multipurpose WordPress themes on the market today. Our list contains a number of best sellers, several newcomers that are proving to be highly popular, and a few themes that are ideal for creating the types of...

  • An Introduction to Static Site Generators

    Static site generators seem to have been becoming more and more popular recently, but they’re not one of those ephemeral novelty things that grow in popularity as quickly as they fall into oblivion shortly after. For over a decade, many different projects — 394 of...

  • Automated Tests for Visual Responsive Layouts

    Today it's all about testing. In 2015, many developers knows about TDD and I personally think that testing is one of the key for quality products. But what about testing in a Front-end environment? How do you guys write your tests for a responsive page or...

  • Getting Dicey With Flexbox

    What if you could build complex CSS layouts in minutes? Flexbox is a new CSS layout spec that makes it easy to construct dynamic layouts. With flexbox, vertical centering, same-height columns, reordering, and direction agnosticism are a piece of cake. There's a popular myth floating around that...