How to flush output after each `echo` call? How to flush output after each `echo` call? php php

How to flush output after each `echo` call?


I've gotten the same issue and one of the posted example in the manual worked. A character set must be specified as one of the posters here already mentioned. http://www.php.net/manual/en/function.ob-flush.php#109314

header( 'Content-type: text/html; charset=utf-8' );echo 'Begin ...<br />';for( $i = 0 ; $i < 10 ; $i++ ){    echo $i . '<br />';    flush();    ob_flush();    sleep(1);}echo 'End ...<br />';


Edit:

I was reading the comments on the manual page and came across a bug that states that ob_implicit_flush does not work and the following is a workaround for it:

ob_end_flush();# CODE THAT NEEDS IMMEDIATE FLUSHINGob_start();

If this does not work then what may even be happening is that the client does not receive the packet from the server until the server has built up enough characters to send what it considers a packet worth sending.


Old Answer:

You could use ob_implicit_flush which will tell output buffering to turn off buffering for a while:

ob_implicit_flush(true);# CODE THAT NEEDS IMMEDIATE FLUSHINGob_implicit_flush(false);


So here'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 = Offzlib.output_compression = Off
  • In nginx.conf:

    gzip  off;proxy_buffering  off;

Also have these lines at hand, especially if you don't have access 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, comment 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 to make the buffer achieve the minimum size in order to flush data    echo str_repeat(' ',1024*64);    sleep(1);}