问题
I have a script that used to work in PHP5.3 to handle buffering for a particular log file but after the server was upgraded to PHP5.5 it no longer works. The output needs to be html so I was hoping to simply flush output after each echo.
This is a cut-down test version of the code that used to work...
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);
set_time_limit(0);
echo 'Start ...<br />';
for( $i = 0 ; $i < 10 ; $i++ )
{
echo $i . '<br />';
flush();
ob_flush();
sleep(1);
}
echo 'End<br />';
I suspect that the @ini_set commands are not over-riding the settings and I am just hoping for a simple example that will flush the output buffers. Most of the examples online are from 6+ years ago and none of them have worked. I read that buffering was re-written in PHP5.4 so I wonder if that is also to blame.
回答1:
I have tested your script and done some fixing/Enhancements
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
// you can dismiss this configuration, the bellow explanation is from the php.ini itself
/* Implicit flush tells PHP to tell the output layer to flush itself
automatically after every output block. This is equivalent to calling the
PHP function flush() after each and every call to print() or echo() and each
and every HTML block.
*/
@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);
set_time_limit(0);
echo 'Start ...<br />';
for( $i = 0 ; $i < 10 ; $i++ )
{
// put the bellow php code if the user browser is Firefox, Internet Explorer or Safari
// Google Chrome just works fine with it but it do not need
echo str_repeat(" ", 1024);
echo $i . '<br />';
flush();
// ob_flush(); you have used flush(), why using ob_flush() there is nothing to flush anymore
sleep(1);
}
echo 'End<br />';
I don't think that the update of PHP version causes the problem, but i am not certain
Hope that helps :)
来源:https://stackoverflow.com/questions/27847938/php-streaming-output-buffering-no-longer-working