Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Consider the following, very simple PHP code:

for ($x = 0; $x < 100; $x++) 
{
    echo "$x \r\n";
    sleep(1);
}

On a standard PHP configuration, this should produce an error 500 or some equivalent to tell the user that the script timed out. I'd like to know if it's possible to make it so that PHP will still output the numbers 0 through some arbitrary X, X likely being the number of seconds that php.ini sets the timeout to be.

In doing research on this, I found this on Stack Overflow: http://stackoverflow.com/questions/2513110/is-there-a-way-to-make-php-progressively-output-as-the-script-executes. Based on the solutions given in the thread, there are ways to achieve this on a case-by-case basis (i.e., you would have to specifically write each script so that it outputs correctly).

A comment mentioned in the above also mentions that you have set the output_buffering php.ini entry to zero in order for it to work, but you still then have to execute an ob_flush() and a flush() to actually output any data.

So, my question is: is it possible to configure PHP in such a way that a script will always be progressively sent to the browser? For simplicity sake, we may want to assume that the web server will send the data to the browser immediately (instead of buffering data, and then sending it). Thanks!

Edit: to clarify, by any script I mean literally any PHP script on the server, and those scripts should not have to call any special functions in order for the progressive output to work.

share|improve this question
    
sleep() does not count against PHP's script timeout value (except when PHP is running under Windows). See set_time_limit() man page: php.net/manual/en/function.set-time-limit.php –  danlefree Mar 11 '11 at 5:25
add comment

2 Answers

up vote 1 down vote accepted

ob_implicit_flush()

Edit: That's what auto_prepend_file is for.

share|improve this answer
    
After re-reading my original question, this is an excellent answer :) I've clarified my original question a little bit, but this is definitely a good answer for developers looking to achieve this. –  Jake Wood Mar 11 '11 at 4:39
add comment

There's actually lots of questions here.

first - script/webserver timeouts - timeouts are there for a good reason - see also http://stackoverflow.com/questions/4678790/how-do-i-set-time-limit-for-loop but what those actualy are, and at what point in the execution of the code they are applicable varies.

second - I suspect your code is actually doing something much more interesting than calling sleep(1) multiple times - how you should go about solving the problem depends on what other things it is doing - if the execution thread is outside of PHP, then you are very limited in what you can do within PHP (this affects memory to - e.g. running a query which returns a massive recordset)

third - if you're already concerned about timeouts, then getting feedback to the user isn't going to resolve any problem with the job not completing due to the amount of time it takes - you should consider running the process independently and polling the progress via Ajax or a refreshing page in PHP. Note that running different processes and threads is a not as trivial as it sounds.

fourth - the only explicit question you asked was "is it possible to configure PHP in such a way that a script will always be progressively sent to the browser?". There are several answers here suggesting you use flush - but that just flushes the buffer to the webserver - whether the webserver decides to send that back to the browser is undefined, and at what stage the browser chooses to render chunks is also undefined - it might work on your current setup - but it's not portable.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.