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!


Comments

  1. Jay

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

  2. Dimitri

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

  3. Will

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

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

  5. httpwebwitch

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

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

  7. DerFichtl

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

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

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

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

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

  11. Custom PHP

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

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

  14. Teo Radu

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

    i want how to increase speed of execution automatically with out using max_execution_time ,set_time_limit ( seconds) methods.

  16. Terri Ann

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

  17. James

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

  18. Verizon Promo Code

    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?

  19. Rahul Modak

    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.

  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: http://www.sudo-juice.com/php-time-limit-extend-script-running-time/

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

  22. Aliza Betker

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


Be Heard!

Share your thoughts without being a jerk! And wrap your code in <code> tags, f00!

Name*:
Email*:
Website: