How to empty php buffer while running?

Asked

Viewed 94 times

3

I want something printed on the screen during execution, and not only when the script is finished, I tried:

<?php

ob_start();

for($i = 0; $i < 5; $i++){

   echo $i . '<br>';
   ob_flush();
   flush();

   sleep(3);
}

but it didn’t work, everything is printed at once at the end of the execution.

1 answer

4


According to my tests, that way it works:

@ini_set("output_buffering", 0);  // off 
@ini_set("zlib.output_compression", 0);  // off
@ini_set("implicit_flush", 1);  // on   

header('Content-Type: text/plain');

ob_implicit_flush(true);

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

    echo $i;

    echo str_repeat(' ',1024*64);

    sleep(1);
}

I got that answer from SOEN

  • Thank you so much! Exactly what I need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.