简体   繁体   中英

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). 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.

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() .

You can never clear out all of the buffers, as PHP is only aware of its own. The server can buffer, and the clients almost always buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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