output buffering

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!

Moderator: General Moderators

Post Reply
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

output buffering

Post 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
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Thank you all for your help. It worked !

Cheers,
Dibyendra
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post 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
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post 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
Post Reply