PHP 流/输出缓冲不再工作


PHP Streaming/Output Buffering no longer working

我有一个脚本,它曾经在 PHP5.3 中处理特定日志文件的缓冲,但在服务器升级到 PHP5.5 后它不再工作。输出需要是 html,所以我希望在每次回声后简单地刷新输出。

这是曾经工作的代码的精简测试版本...

@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 />';

我怀疑@ini_set命令没有覆盖设置,我只是希望有一个简单的示例来刷新输出缓冲区。网上的大多数例子都是来自6 +年前,没有一个奏效。我读到缓冲是在 PHP5.4 中重写的,所以我想知道这是否也是罪魁祸首。

我已经测试了您的脚本并做了一些修复/增强功能

@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 />';

我不认为PHP版本的更新会导致问题,但我不确定
希望对:)有所帮助