Luca Matteis
Luca Matteis

Reputation: 29267

PHP flushing output as soon as you call echo

I thought flush(); would work, at least from what Google/Stackoverflow tell me, but on my Windows WAMP (Windows, Apache, MySQL, PHP) system it doesn't work.

Is there some PHP setting I have to set to make flush() work?

Here's my code:

<?php
echo "Fun";

flush();

sleep(5);

echo "<br>Mo";
?>

The code just outputs all together when the script is done executing (after 5 seconds).. I don't want this, I want 'Fun' to show up right away, and then after 5 seconds 'Mo'.

I've tried other combinations of flush like ob_end_flush(); or ob_implicit_flush(true); but nothing is working. Any ideas?

Upvotes: 0

Views: 14446

Answers (6)

Roger
Roger

Reputation: 8576

So that's what I found out:

Flush would not work under Apache's mod_gzip or Nginx's gzip because, logically, it is gzipping the content, and to do that it must buffer content to gzip it. Any sort of web server gzipping would affect this. In short, at the server side, we need to disable gzip and decrease the fastcgi buffer size. So:

  • In php.ini:

    . output_buffering = Off

    . zlib.output_compression = Off

  • In nginx.conf:

    . gzip off;

    . proxy_buffering off;

Also have this lines at hand, specially if you don't have acces to php.ini:

  • @ini_set('zlib.output_compression',0);

  • @ini_set('implicit_flush',1);

  • @ob_end_clean();

  • set_time_limit(0);

Last, if you have it, coment the code bellow:

  • ob_start('ob_gzhandler');

  • ob_flush();

PHP test code:

ob_implicit_flush(1);

for($i=0; $i<10; $i++){
    echo $i;

    //this is for the buffer achieve the minimum size in order to flush data
    echo str_repeat(' ',1024*64);

    sleep(1);
}

Upvotes: 7

Philipp
Philipp

Reputation: 45

If the problem persists, although you explicitly set

implicit_flush = yes 

in your php.ini, you might also want to set

output_buffering = off

which did the trick in my case (after pulling my hair for 4+hrs)

Upvotes: 1

Vincent
Vincent

Reputation: 2404

If you're using CGI/FastCGI, forget it! These don't implement flush. The Webserver might have it's own buffer.

You can disable all output buffering in PHP with following command:

ob_implicit_flush();

Upvotes: 3

jmz
jmz

Reputation: 5479

The script works fine from CLI, displaying "Fun", waiting 5 secs before displaying "<br>Mo".

For a browser the results might be a bit different because:

  1. The browser wont start rendering right away. Getting 3 bytes of data for HTML document isn't enough to do anything, so it'll most likely wait for a few more.
  2. Implicit IO buffering on the lib level will most likely be active until a newline is received.

To work around 1) use text/plain content type for your test; 2) needs newlines, so do an echo "Fun\n"; and echo "<br>Mo\n"; Of course you wont be using text/plain for real HTML data.

Upvotes: 5

Savageman
Savageman

Reputation: 9477

Maybe the problem is Apache here, which also may have buffers...

Upvotes: 0

tamasd
tamasd

Reputation: 5913

Check your php.ini for output_buffering.

Upvotes: 0

Related Questions