PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Dear all,
How can we output the buffer as soon as we get error and before script stop running ? I have tried to outpur the buffer by following code but didn't work .
Does anybody have an idea how to flush the output whenever we print anything on the screen and continue running the script ?
Any help will be appreciated ?
<?php
if (ob_get_level() == 0) ob_start();
for ($i = 0; $i<10; $i++){
echo "<br> Line to show.";
echo str_pad('',4096)."\n";
ob_flush();
flush();
sleep(2);
}
echo "Done.";
ob_end_flush();
?>
Tested and working in FF2
EDIT: I meant to mention that the problem is that the browser will only render content after it receives a certain number of bytes from the server -- that's where the str_pad() call comes in.
Web servers can implement their own output buffering that may get in the way of your wish to output progress on-demand. Some wait for a certain amount of data (or the script to end) to be prepared before they'll send the first set of packets.
If you are using Opera it will render output as SOON as it gets the information flushed to it by the server. Other browsers like tend to WAIT until it gets the entire HTML page before it renders and there may be a configuration setting to disable the wait. And others still will render as each newline or <BR> is received.
The thing is you can't always rely on the flush or ob_flush to force a browser to render the data as soon as it gets it.
As example given by aaronhall, I tried to implement on my source code and it worked. I use mozilla firefox in linux. I have n't tried on other browsers like ephinany, opera etc. I'll post the result as soon as I try on other browsers.