Flush buffers

(post was updated on 2014/07/31)

Sometimes you want to output HTML to the browser before a script has finished completely. In some browsers it takes a while before ‘some’ content is displayed. Flush buffers can help to actively force output to be shown.

A combination of PHP functions is useful to please all kinds of browsers. However the code below might no longer work in recent Apache/PHP environments. Still works locally on XAMPP

function flush_buffers(){
	ob_end_flush();
	ob_flush();
	flush();
	ob_start();
}

You should read http://stackoverflow.com/questions/4870697/php-flush-that-works-even-in-nginx. Maybe these adjustments will help you to implement the PHP flush functions.

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);
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.