Page 1 of 1

output buffering

Posted: Mon Oct 30, 2006 5:43 am
by dibyendrah
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 .

script that I'm testing is :

Code: Select all

ob_start();
$x =0;
while($x<10){
flush();
print $x;
ob_flush();
flush();
$x++;
sleep(1);
}
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 ?

Thank you.

With best regards,
Dibyendra

Posted: Mon Oct 30, 2006 6:42 am
by aaronhall
Found this in a comment on the manual (http://us3.php.net/manual/en/function.flush.php#54841)

Code: Select all

<?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.

Posted: Mon Oct 30, 2006 6:49 am
by aaronhall
Okay -- turns out I have no idea why your code isn't working. The script I posted works fine without the str_pad call in FF2...

Posted: Mon Oct 30, 2006 5:53 pm
by feyd
ob_end_flush() is normally all that's needed.

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.

Posted: Mon Oct 30, 2006 10:49 pm
by dibyendrah
Thank you all for your help. It worked !

Cheers,
Dibyendra

Posted: Mon Oct 30, 2006 11:14 pm
by AKA Panama Jack
Actually that can also depend upon the browser.

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.

Posted: Tue Oct 31, 2006 12:38 am
by dibyendrah
So, what will be the very best solution for making every browser to output the same as soon as it gets data to output ? Thank you all.


Cheers,
Dibyendra

Posted: Tue Oct 31, 2006 2:30 am
by dibyendrah
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.

Thank you.
with regards,
Dibyendra