php output buffer

Status
Not open for further replies.

ozonew4m

ThisTimeNextYearRodders
Jul 6, 2007
235
3
0
At my desk!
I have a few php scripts that can take quite a while to complete but i want to be able to output progress to the page mid script so i can keep track of what it is doing..

i can do it via ajax from mysql but i was just wondering if there was something in php i could use..

at the moment nothing will output to the page until the script completes..

?
 


There is a trick that you can use that does not require output buffering. Basically, it is to send some whitespace to the browser, then flush it. The whitespace (which does not show up) is for some versions of IE, as noted below:

Some versions of Microsoft Internet Explorer will only start to display the page after they have received 256 bytes of output, so you may need to send extra whitespace before flushing to get those browsers to display the page.

Code:
echo str_repeat(" ", 256);
flush();
ob_flush();

Stick that in a function, and then call it when you want to output the data.

Note: IE will NOT display a table that is not complete. This flush will not help with that. You would have to end the table and then flush.

Other than that, this works great. I use it to display that the script is still processing, even if there is no output (echoing '....' to show progress). I put the ECHO '.'; before the call to the function.

Of course, you can use it without a function too. Just saves typing...
 
Status
Not open for further replies.