Php – Allowing PHP to “echo” as the Script Runs

PHP

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

Best Answer

ob_implicit_flush()

Edit: That's what auto_prepend_file is for.

Related Topic