PHP Streaming/Output Buffering no longer working

荒凉一梦 提交于 2019-12-12 03:16:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!