PHP输出缓冲区失败

时间:2012-02-04 04:27:35

标签: php windows cmd system buffer

就像演示一样,请尝试以下代码:

<?php
echo '<pre>';      
ob_end_flush();
for($i=0;$i<2;$i++) {
passthru("ping -n 8 127.0.0.1");
@ob_flush();
}
echo '</pre>';
?>

这是输出:

Pinging stackoverflow.com [64.34.119.12] with 32 bytes of data:
Reply from 64.34.119.12: bytes=32 time=28ms TTL=56
Reply from 64.34.119.12: bytes=32 time=29ms TTL=56
Reply from 64.34.119.12: bytes=32 time=29ms TTL=56
Reply from 64.34.119.12: bytes=32 time=28ms TTL=56

Ping statistics for 64.34.119.12:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 28ms, Maximum = 29ms, Average = 28ms

Pinging stackoverflow.com [64.34.119.12] with 32 bytes of data:
Reply from 64.34.119.12: bytes=32 time=28ms TTL=56
Reply from 64.34.119.12: bytes=32 time=27ms TTL=56
Reply from 64.34.119.12: bytes=32 time=26ms TTL=56
Reply from 64.34.119.12: bytes=32 time=29ms TTL=56

Ping statistics for 64.34.119.12:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 26ms, Maximum = 29ms, Average = 27ms

它实际上做了两次(来自“小于2”的循环)。如果您测试脚本,您会注意到第一个脚本需要几秒钟,然后它会立即输出。在那之后(第二次循环),它逐行进行。我的目标是只有一个输出,一行一行而不缓冲任何输出,如下所示,除非发生这种情况:

<?php
system("ping -n 8 127.0.0.1");
?>

注意:这是在使用PHP 5的Windows服务器上运行。

1 个答案:

答案 0 :(得分:1)

所以,这里发生的事情是你的浏览器可能正在缓冲。我想如果你看一下数据包嗅探器,你的冲洗可能正常。

为确保这一点,请致电flush()以及ob_flush()

你永远无法清除所有缓冲区,因为PHP只知道它自己的缓冲区。服务器可以缓冲,客户端几乎总是缓冲。