简体   繁体   中英

Problems with PHP Output Buffer Flush

I have a PHP script which takes a while to process and I wanted to get the site to display it's progress while it is running.

I have been having problems displaying the contents of the output buffer while a PHP code is running. I have been trying the use of ob_implicit_flush(true) , ob_end_flush() and ob_flush() with no success.

After looking for a fair bit at the PHP manual on php.net I have come across a comment by Lee saying

As of August 2012, all browsers seem to show an all-or-nothing approach to buffering. In other words, while php is operating, no content can be shown.

In particular this means that the following workarounds listed further down here are ineffective:

1) ob_flush (), flush () in any combination with other output buffering functions;

2) changes to php.ini involving setting output_buffer and/or zlib.output_compression to 0 or Off;

3) setting Apache variables such as "no-gzip" either through apache_setenv () or through entries in .htaccess.

So, until browsers begin to show buffered content again, the tips listed here are moot.

see here

If he is correct, is there an alternative I can use to display the progress of the script running?

[edit]

Looks like Lee could be right. I have been doing some testing and here is an example of the problem

<?php
ob_implicit_flush(true);
ob_end_flush();

for ($i=0; $i<5; $i++) {
   echo $i.'<br>';
   sleep(1);
}
?>

Should display 0 to 4 with a second in between. What actually happens is that the site waits 5 seconds then displays 0 to 4 immediately. It is waiting for the script to complete then displays everything in one go.

I have had output_buffering = Off in my php.ini file and tried setting it to 1 and 4096 (4096 being 4KB) and it still does the same.

Check the buffer size and make an "empty" dummy of that size, for example...

$flushdummy="";
for ($i=0; $i<700; $i++) { $flushdummy=$flushdummy."      "; }
$flushdummy=$flushdummy."\n";

then, when output is desired, add the dummy to the output, plus the flush:

echo "here I am<br />", $flushdummy;
flush();
@ob_flush(); 

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