CodeIgniter Forums
Stream Output buffer - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Stream Output buffer (/showthread.php?tid=60942)



Stream Output buffer - El Forum - 08-06-2014

[eluser]Unknown[/eluser]
hi all, i have a long task running ( ftp uploads )
and i need enable output flushing how i can do it with CI ?

im trying with ob_flush (tried also to se implicit flush ) but without success

any one have a solution?


Stream Output buffer - El Forum - 08-06-2014

[eluser]CroNiX[/eluser]
Here's a helper I created to do that:

Code:
//kill CI's buffered output and stream realtime
if ( ! function_exists('kill_buffer'))
{
function kill_buffer()
{
  $CI =& get_instance();
  //output any CI output up til now
  echo $CI->output->get_output();
  
  //output/wipe buffer and enable implcit flush
  @ob_end_flush();
   ob_implicit_flush(TRUE);
  
   //For some reason about 1kb needs to be sent before realtime output shows up.  Just send a bunch of spaces in a hidden div.
   echo '<div >' . str_repeat('&nbsp;', 170) . '</div>';

   //No headers beyond this point unless they are sent FIRST...
}
}

Depending on your usage, you might need to adjust the very last output (like you wouldn't want to output html to the CLI), but I use it in a browser so that's how I wrote it.

To test you could just do:
Code:
kill_buffer();
for ($x = 0; $x < 5; $x++)
{
  echo date('H:i:s') . '<br>';
  sleep(1);
}
should write the time in real time to the browser 5 times, 1 second apart.