|
 |
flush (PHP 3, PHP 4, PHP 5) flush -- Flush the output buffer Descriptionvoid flush ( void )
Flushes the output buffers of PHP and whatever backend PHP is
using (CGI, a web server, etc). This effectively tries to push
all the output so far to the user's browser.
flush() has no effect on the buffering
scheme of your webserver or the browser on the client
side.
Thus you need to call both ob_flush() and
flush() to flush the output buffers.
Several servers, especially on Win32, will still buffer
the output from your script until it terminates before
transmitting the results to the browser.
Server modules for Apache like mod_gzip may do buffering of their own
that will cause flush() to not result in data being
sent immediately to the client.
Even the browser may buffer its input before displaying it.
Netscape, for example, buffers text until it receives an
end-of-line or the beginning of a tag, and it won't render
tables until the </table> tag of the outermost table is
seen.
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.
flush
vlad at modomail dot com
05-Apr-2006 06:46
Sorry if this is off topic, but it's the closest place I could find :) I had an issue with essentially running a CPU intensive task while updating the browser with a progress bar via javascript and flushing the buffer a lot.
When the script was running, it effectively blocked other pages from running. I had a few extra httpd_preforks processes just waiting to be used, but they just sat there. I wasn't reading a file, locking database tables or anything that I would suspect an exclusive lock on, just outputting a bunch of text to the browser. I even tried throttling my loops to see if it was processor related but still had problems.
Finally, I found this in the php.ini file and changed the On to Off and it worked.
[Sockets]
; Use the system read() function instead of the php_read() wrapper.
sockets.use_system_read = Off
Posting in case someone else has the same issue (or if someone knows why this would make a difference :) ).
mega023 at gmail dot com
25-Nov-2005 03:54
If flush is not working probably mod_gzip is enabled.
To disable it just add following lines to .htaccess
<IfModule mod_gzip.c>
mod_gzip_on no
</IfModule>
PuTTYshell
20-Nov-2005 07:06
<?php
header('Content-type: multipart/x-mixed-replace;boundary=endofsection');
print "\n--endofsection\n";
$pmt = array("-", "\\", "|", "/" );
for( $i = 0; $i <10; $i ++ ){
sleep(1);
print "Content-type: text/plain\n\n";
print "Part $i\t".$pmt[$i % 4];
print "--endofsection\n";
ob_flush();
flush();
}
print "Content-type: text/plain\n\n";
print "The end\n";
print "--endofsection--\n";
?>
Server Push with "multipart/x-mixed-replace", tested on Firefox 1.07.
This is an example requiring both ob_flush and flush.
mikael at oebb dot net
03-Oct-2005 01:47
Hi all.
Been scratching my head over data NOT flushed to IE (6) even though I tried strpad 4096 chars, all headers OK, TABLE and no TABLE, flush and ob_flush - still a blank page. Tried adding a sleep(1) before flushing - and everything worked as a charm.
/Mikael
20-Sep-2005 12:37
In my testing, Internet Explorer 6.0 wouldn't flush anything nested in <table> or <td> tags, regardless of padding. But at the <body> level everything flushed with no fuss -- no padding or tags required.
Both Firefox 1.0 and Safari 2.0 could flush within tables, and both required a tag after the text (like <br>). Safari could flush only after the first 1024 characters where received. Firefox needed at least 8 characters per flush (but it could flush anything at the <body> level).
So the only thing that worked on all those browsers was this:
<html>
<body>
<?php echo str_pad('',1024); for ($i=10; $i>0; $i--) {
echo str_pad("$i<br>\n",8);
flush(); sleep(1);
}
?>
</body>
</html>
StanV
08-Aug-2005 01:15
So will this:
<?php
for ($i = 0; $i<10; $i++){
echo "<br> Line to show.";
echo str_pad('',4096)."\n";
flush();
sleep(2);
}
echo "Done.";
?>
I've not found a case where I need to use ob_flush() if I never started caching the buffer in the first place.
There seems to be some confusion you have to start buffering the output to make sure you flush it later with both commands.
If you don't need to buffer with ob_*, only flush() is enough in my experience.
js at jeansebastien dot com
16-Jul-2005 11:41
This will show each line at a time with a pause of 2 seconds.
(Tested under IEx and Firefox)
<?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();
?>
Colin at ColinsRealm dot Com
13-Jul-2005 06:54
I find that you need to open your ob for it to print . Heres my example that works in IE . Yes - the strings need to be long for it to flush ...
ob_start();
for($i = 1; $i <= 10; $i++){
$sourceName = 'filetoupload' . $i;
$imageArray = array();
if(!is_uploaded_file($_FILES[$sourceName]['tmp_name'])) { $imageArray[$num-1] = NULL; $message="Image $i does not exist (has not been prompted for upload)<br/>"; }
else {
move_uploaded_file($_FILES[$sourceName]['tmp_name'], "temporary/$sourceName");
$message = "Image $i has been uploaded to the server";
}
echo $message;
flush();
ob_flush();
usleep(300);
}
ob_end_flush();
Vladimir Kornea of typetango.com
02-Jun-2005 07:37
The documentation for flush() states:
flush() has no effect on the buffering scheme of your webserver or the browser on the client side. Thus you need to call both ob_flush() and flush() to flush the output buffers.
What it does not state is the order in which the functions should be called. This is the correct order:
ob_flush();
flush();
These functions do not need to be called repeatedly (as other have stated), merely in the correct order.
MOELZE T GMX DOt DE (Michael)
15-May-2005 04:38
It is a bit complicated to work with the funktion flush() and you have to experiment with it a bit.
So if you design a site which has a timeloop at the end that calls a other site via a form data input (Data Submit) you have
to give something out to the buffer to get that new site loaden quick.
For example:
$instant=gettimeofday();
$timenow=$instant["sec"];//Start Time
//timeloop(e.g. for security_save after 30 min)
while (1) { echo "<b></b>";//Useless (only to quickload next
//or same Site when do a switch)
flush(); //giveout buffer
$instant=gettimeofday();
$timeactual=$instant["sec"]; //get Actual Time in Secs
$flag=(($timeactual>$timenow+$diff)? 1:0);//$diff=switchTime
if ($flag) { what_do_at_switch_Time();//Sec.Save etc.etc.
$timenow=$timeactual; } //Set new Start Time
sleep(5); //Or so...(Important)
} //End of while-Loop
So you can programm a security save or other function in your site and if you do a switch the upload of the new or same site (the called site) works...
Alex
22-Apr-2005 02:35
Modified progress bar.. Works in IE, Mozilla+FF.
<html>
<head>
<style type="text/css"><!--
div {
margin: 1px;
height: 20px;
padding: 1px;
border: 1px solid #000;
width: 275px;
background: #fff;
color: #000;
float: left;
clear: right;
top: 38px;
z-index: 9
}
.percents {
background: #FFF;
border: 1px solid #CCC;
margin: 1px;
height: 20px;
position:absolute;
width:275px;
z-index:10;
left: 10px;
top: 38px;
text-align: center;
}
.blocks {
background: #EEE;
border: 1px solid #CCC;
margin: 1px;
height: 20px;
width: 10px;
position: absolute;
z-index:11;
left: 12px;
top: 38px;
filter: alpha(opacity=50);
-moz-opacity: 0.5;
opacity: 0.5;
-khtml-opacity: .5
}
-->
</style>
</head>
<body>
<?php
if (ob_get_level() == 0) {
ob_start();
}
echo str_pad('Loading... ',4096)."<br />\n";
for ($i = 0; $i < 25; $i++) {
$d = $d + 11;
$m=$d+10;
echo '<div class="percents">' . $i*4 . '% complete</div>';
echo '<div class="blocks" style="left: '.$d.'px"> </div>';
flush();
ob_flush();
sleep(1);
}
ob_end_flush();
?>
<div class="percents" style="z-index:12">Done.</div>
</body>
</html>
storm at stormfx dot com
16-Apr-2005 08:25
Remember when trying to output partial content, that PHP's .ini file setting affects whether or not it succeeds, depending on how you code it, of course. Here's an example using a progress bar done soley in PHP:
// Some HTML/CSS to spice up the spaces...
<html>
<head>
<style type="text/css"><!--
.pbar {
background: #EEE;
border: 1px solid #CCC;
margin: 1px;
height: 10px;
width: 10px;
}--></style>
</head>
<body>
<?php
if (ob_get_level() == 0) {
ob_start();
}
echo str_pad('Loading... ',4096)."<br />\n";
for ($i = 0; $i < 25; $i++) {
echo '<span class="pbar"> </span>';
ob_flush();
usleep(700000);
}
ob_end_flush();
?>
</body>
</html>
Note: This is just an example. Normally it's bad practice to use the OB like this :)
This was tested with PHP 5.04 on WinXP Pro/IIS 5.1
Marty
14-Apr-2005 08:33
This is an extention of Rusty's comment below:
After sitting here for hours trying to make IE6 flush data out in the middle of a page (with it working perfectly in Firefox), I finally figured out the problem. IE will not display flushed data (even if it has it) unless the table that contains it is complete.
Every new element you want IE to display must not be in ANY kind of table at all. You must end all your tables.
Ghostshaw at spymac dot com
07-Apr-2005 04:45
I would like to point out that there is a function to replace ob_flush and flush. If you set ob_implicit_flush(true); at the top of the page it will automatically flush any echo or print you do in the rest of the script.
Note that you still need a minimum amount of data to come through the browser filter. I would advice using str_pad($text,4096); since this automatically lenghtens the text with spaces to 4 KB which is the minimum limit when using FireFox and linux.
I hope this helps you all out a bit.
matt at nospamplease dot hevanet dot com
21-Mar-2005 10:42
Like IE, Safari needs a fair amount of data before it'll display anything, actually more than explorer. The following code works for me in Firefox and Safari, and should work in IE as well.
<?php
for($i = 0; $i < 40000; $i++)
{
echo ' '; }
flush();
usleep(50000);
?>
This code came from a comment on a blog discussing browser functionality with flush();
crmacd at yahoo dot com
04-Mar-2005 03:28
I figured out a way to create a simple progress bar that is for the most part cross platform. Seeing as I got my ideas from this site it's only share to give back to the community.
Note: Something interesting about browser buffering... you have to have the <html><body> for Firefox and some other browsers to recognize items by their id in Javascript. So I recommend using some sort of header function before calling this function.
<?php
function fn_progress_bar($intCurrentCount = 100, $intTotalCount = 100)
{
static $intNumberRuns = 0;
static $intDisplayedCurrentPercent = 0;
$strProgressBar = '';
$dblPercentIncrease = (100 / $intTotalCount);
$intCurrentPercent = intval($intCurrentCount * $dblPercentIncrease);
$intNumberRuns++;
if(1 == $intNumberRuns)
{
$strProgressBar = <<< BAR
<table width='50%' id='progress_bar' summary='progress_bar' align='center'><tbody><tr>
<td id='progress_bar_complete' width='0%' align='center' style='background:#CCFFCC;'> </td>
<td style='background:#FFCCCC;'> </td>
</tr></tbody></table>
<script type='text/javascript' language='javascript'>
function dhd_fn_progress_bar_update(intCurrentPercent)
{
document.getElementById('progress_bar_complete').style.width = intCurrentPercent+'%';
document.getElementById('progress_bar_complete').innerHTML = intCurrentPercent+'%';
}
</script>
BAR;
}
else if($intDisplayedCurrentPercent <> $intCurrentPercent)
{
$intDisplayedCurrentPercent = $intCurrentPercent;
$strProgressBar = <<< BAR
<script type='text/javascript' language='javascript'>
dhd_fn_progress_bar_update($intCurrentPercent);
</script>
BAR;
}
if(100 <= $intCurrentPercent)
{
$intNumberRuns = $intDisplayedCurrentPercent = 0;
$strProgressBar = <<< BAR
<script type='text/javascript' language='javascript'>
document.getElementById('progress_bar').style.visibility='hidden';
</script>
BAR;
}
echo $strProgressBar;
flush();
ob_flush();
}
?>
siggi AT june systems DOT com
04-Mar-2005 07:54
After searching through the PHP site, google and various forums, not finding a solution to my script not outputting anything while calling flush and ob_flush, I thought of trying to tell PHP to call:
session_write_close();
before starting echo'ing. It worked like a charm. I couldn't find any references to this, so I hope this note will help someone in the future.
Squ1sher
01-Feb-2005 12:25
On Windows xampp 1.3 with php 4.3.4 is use this functions
to force a flush.
<?php
function dummyErrorHandler ($errno, $errstr, $errfile, $errline) {
}
function forceFlush() {
ob_start();
ob_end_clean();
flush();
set_error_handler("dummyErrorHandler");
ob_end_flush();
restore_error_handler();
}
?>
ob_end_flush generates a warning, which is supressed, using the dummy-errorhander. You could also use @, but then nusphere will also print the warning.
mat.wilmots (at) wanadoo (dot) fr
10-Nov-2004 04:26
This code does not work as expected
<pre>
<?php
echo "Test1\n";
flush();
sleep(3);
echo "Test2\n";
?>
</pre>
It should print Test1, wait 3 seconds then print Test2 but actually it prints everything at the end.
It seems PHP is doing output buffering on its own, here is the way to make it work :
<pre>
<?php
ob_end_flush();
echo "Test1\n";
flush();
sleep(3);
echo "Test2\n";
?>
</pre>
You don't really need to repeat some "fake" text to fill the buffer in order to make this effect.
matt (at) roughest (dot) net
05-Oct-2004 04:57
Something about the Linux version of Firefox makes flush() not work. Not sure if this is specifically a Firefox problem, or something about the way Linux handles HTTP streams, but nothing gets rendered until the HTTP stream is closed.
Interestingly, flush() works fine running IE on Linux under Wine.
Rusty
24-Feb-2004 09:25
Netscape will flush the output as expected at the point it is called from within your script. IE, however, needs a boost.
IE seems to have a condition where it will flush data when it hits an end (</table>) tag AND has at least 256 chars of data.
So, pad your output with necessary spaces, wrap your progressing data around open (<table>) and end (</table>) tags, and then call flush() so that one script will work for Netscape as well.
TESTING ENVIRONMENT
IE: Ver/6.0.2800
Netscape: Ver/7.1
PHP: Ver/4.3.4
Apache: Ver/1.3.27
phpnote at mijav dot dk
23-Jan-2004 03:46
If you are having trouble makeing flush(); or ob_flush(); work the way you want, try both of them.
Using PHP 4.3.3RC1 there was no effect using flush() or ob_flush(), however a combination of both gave me perfect results.
Example:
<?
echo str_repeat(" ", 256);
$i = 100;
while($i > 0)
{
echo $i." bottle".($i != 1 ? 's' : '')." of beer on the wall, ".$i." bottle".($i != 1 ? 's' : '')." of beer. Take one down and pass it around, ".(--$i)." bottle".($i != 1 ? 's' : '')." of beer on the wall<br>\n";
flush();
ob_flush();
usleep(50000);
}
?>
This could be the result of a server-issue - maybe you are using a public server that has OB enabled by default.
viazenetti
20-Jan-2004 12:25
Note to my last posting:
Since version 4.2.0 you can use ob_flush() instead. This works fine with sessions and cookies disabled.
m@rco
29-Oct-2003 07:22
in order to display in HTML pages something like a "progress bar" or if you want to force the web server to flush output to the browser, or the browser to flush as well, you can print a long enough "dummy" string to make the output buffer grow, as you probably know, like:
print "[";
for($i = 0; $i < 100; $i++){
$spaces.=" ";
} // for
//and then
for($i = 0; $i < 10; $i++){
for($ii = 0; $ii < 200000; $ii++){
//do something slow here
} // for
print "$spaces|";
flush();
} // for
print "]";
/*
but this somethin may be not really what you expect in
a progress bar, as it prints spaces (although rendered as single by the browser) between the bar units..
you can solve this using
$spaces.="<!-- bufferme -->";
/*
print "[";
for($i = 0; $i < 100; $i++){
$spaces.="<!-- bufferme -->";
} // for
//and then
for($i = 0; $i < 10; $i++){
for($ii = 0; $ii < 200000; $ii++){
//do something slow here
} // for
print "$spaces|";
flush();
} // for
print "]";
//which looks nice as a progress bar..
scottmacvicar at ntlworld dot com
30-Sep-2003 07:40
Regarding Apache2
flush() will produce the same results as ob_flush() when output buffering is involved.
jthome at fcgov dot com
23-Jul-2003 01:52
This may not be obvious from the previous posts.
You must flush all HTML up to and including the <body> tag (<head>, <meta>, etc.), or the browser won't respond. This would have the effect of "blanking" the page while waiting for the script to complete. This is in addition to the other tips posted.
Most users expect something immediate to happen when they click on a link. "Blanking" the page is often times better for those not accustomed to keeping an eye on their browser's progress bar.
| |