Prevent script timeout

Discussion in 'PHP' started by Fracisc, Sep 1, 2014.

  1. #1
    I have a script like this:
    foreach ($test as $img => $name ) {
    // do stuff to 10000s images
    }

    Obviously, the script cannot finish like this. The problem is that my hosting company will not let me echo stuff in realtime. The result is output all at once.

    So, basically, my question is, how can I run this script in these conditions? Maybe split the foreach in pieces? But I need to be able to copy the output text after each run an only then go to the next set of images.

    Any idea would be appreciated.

    Thanks!
     
    Fracisc, Sep 1, 2014 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    You can try doing it like vb forums does it. Do 100 at a time, then echo output, then redirect (via js) to the second patch. I always found that annoying and broke on my many times, but that would be a solution. You can always using CLI for this instead of the browser.
     
    ThePHPMaster, Sep 1, 2014 IP
  3. funzie

    funzie Well-Known Member

    Messages:
    71
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    123
    #3
    I would suggest doing it offline, however if you must do it in real time you can change the time out or script execution limit, for example http://us1.php.net/manual/en/function.set-time-limit.php. All setting related to script execution can be set at run time.
     
    funzie, Sep 1, 2014 IP
  4. abhicyco

    abhicyco Active Member

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #4
    set_time_limit(0) will do your work.
     
    abhicyco, Sep 2, 2014 IP
  5. Ratty

    Ratty Active Member

    Messages:
    565
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    95
    Digital Goods:
    1
    #5
    You could try outputting the results to a file rather than the screen that way you could run it in blocks.
     
    Ratty, Sep 2, 2014 IP
  6. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #6
    Could you please give a code example, Ratty?
     
    Fracisc, Sep 2, 2014 IP
  7. funzie

    funzie Well-Known Member

    Messages:
    71
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    123
    #7
    Outputting it a file will still time out as PHP will exit once it reaches the max execution time. If you already have the list of files in a file or a database, then you could do it a piece at a time.
     
    funzie, Sep 2, 2014 IP