【问题标题】:PHP Streaming/Output Buffering no longer workingPHP 流/输出缓冲不再工作
【发布时间】:2015-01-08 19:22:23
【问题描述】:

我有一个脚本曾经在 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 重写的,所以我想知道这是否也是罪魁祸首。

【问题讨论】:

  • 您是否考虑到某些浏览器需要至少看到 1024 字节的输出才能发送任何内容?冲还是不冲?

标签: php output-buffering


【解决方案1】:

我已经测试了您的脚本并进行了一些修复/增强

@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版本的更新导致问题,但我不确定
希望有帮助:)

【讨论】:

  • 嗯。在 FireFox 或 Chrome 中仍然不适合我。我在某处读到@ini-set 可能实际上不起作用,所以也许我需要在 .htaccess 文件中设置这些。感谢您的回复和出色的评论。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多