PHP Flush() not working in Chrome

Kavin Anbazhagan picture Kavin Anbazhagan · May 14, 2011 · Viewed 13.8k times · Source

I stumbled upon this function which promised to work across IE, FF & Chrome. But it does not work in Chrome. Is there a work around?

function buffer_flush(){

    echo str_pad('', 512);
    echo '<!-- -->';

    if(ob_get_length()){

        @ob_flush();
        @flush();
        @ob_end_flush();

    }

    @ob_start();
}

Answer

meetar picture meetar · Aug 1, 2011

Here's how I got flush() working in a while loop in Chrome 12.0.742.122 with PHP 5.3.6:

echo("<html><body>");
while(1) {
  echo(str_pad($my_string_var,2048," "));
  @ob_flush();
  flush();
}

Using a lesser str_pad value worked too, but it would take a bit longer for the first output to appear. If any of the other lines were missing, nothing would appear.

The "@" isn't strictly necessary, but it prevents the log from filling up with "nothing in the buffer" notices.

And of course if you have a pre-existing page, just make sure the <html> and <body> tags are in there; I was writing a page from scratch.