简体   繁体   English

PHP输出缓冲区失败

[英]PHP output buffer fails

Just as a demo, try the following code: 作为演示,请尝试以下代码:

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

This is the output: 这是输出:

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

It actually does it twice (which is from the "is less than 2" loop). 它实际上执行了两次(来自“小于2”循环)。 If you test the script, you'll notice that it takes a few seconds to do the first one, then it outputs it all at once. 如果测试脚本,您会注意到第一个脚本花了几秒钟,然后立即全部输出。 After that (second loop) it goes line by line. 之后(第二个循环),它逐行进行。 My goal is just to have a single output, go line by line and not buffer any output, like the following does except as it happens: 我的目标是只有一个输出,逐行进行而不是缓冲任何输出,就像发生以下情况一样:

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

Note: This is running on a Windows server with PHP 5. 注意:此程序在具有PHP 5的Windows服务器上运行。

So, what is happening here is your browser is likely buffering. 因此,这里发生的是您的浏览器可能正在缓冲。 I think if you look at a packet sniffer, your flushes are probably working. 我认为,如果您查看数据包嗅探器,则刷新可能会起作用。

Just to be sure, call flush() as well as ob_flush() . 只是要确保,调用flush()以及ob_flush()

You can never clear out all of the buffers, as PHP is only aware of its own. 您永远无法清除所有缓冲区,因为PHP仅知道自己的缓冲区。 The server can buffer, and the clients almost always buffer. 服务器可以缓冲,而客户端几乎总是缓冲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM