PHP Doku:: Leert (sendet) den Ausgabepuffer - function.flush.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDas Verhalten von PHP beeinflussenAusgabepufferungOutput-Control-Funktionenflush

Ein Service von Reinhard Neidl - Webprogrammierung.

Output-Control-Funktionen

<<Output-Control-Funktionen

ob_clean>>

flush

(PHP 4, PHP 5)

flush Leert (sendet) den Ausgabepuffer

Beschreibung

void flush ( void )

flush() sendet den Inhalt des Ausgabepuffers und des darunterliegenden PHP Backends (CGI, Apache, ...). Die bisherigen Ausgaben werden damit soweit wie möglich in Richtung des Users weitergereicht.

flush() hat keinen Einfluss auf das Pufferverhalten des Webservers oder des Browsers auf der Clientseite. Es has auch keinen Einfluss auf mit ob_start() erstellte Ausgabebuffer. Daher müssen Sie sowohl ob_flush() als auch flush() aufrufen, um die Ausgabebuffer zu leeren.

Einige Webserver, besonders auf Windows (Win32), puffern ihre Scriptausgaben weiterhin bis zum Scriptende bevor sie sie zum Browser übertragen.

Auch Apache-Module wie mod_gzip puffern zum Teil Ausgaben und verhindern so, dass Ausgaben nach flush() direkt zum Benutzer weitergegeben werden.

Selbst Browser können empfangene Ausgaben puffern bevor sie sie tatsächlich darstellen. Netscape Browser z.B. puffern Text bis sie entweder einen Zeilenwechsel oder den Anfang eines Tags empfangen und zeigen Tabellen erst nach dem Empfang des </table> Tags der äußersten Tabelle.

Einige Versionen des Mircrosoft Internet Explorers beginnen erst dann mit der Ausgabe wenn sie mindestens 256 Bytes empfangen haben. Sie müssen daher eventuell zusätzliche Leerzeichen in Ihre Ausgaben einfügen um eine Darstellung vor Scriptende auf solchen Browsern zu erzwingen.

Rückgabewerte

Es wird kein Wert zurückgegeben.


38 BenutzerBeiträge:
- Beiträge aktualisieren...
seb dot field at gmail dot com
18.09.2010 12:52
If flush() function does not work. You must set next options in php.ini like:

--[code]--
 output_buffering = Off 
 ;output_handler =  
 zlib.output_compression = Off 
 ;zlib.output_handler =  
--[^code^]--

If things does not work you must view headers from the server and check `Server` string.
In my case, as the frontend was Nginx webserver and Apache work as backend.
Accordingly, buffering must be disabled in Nginx config file.
To stop buffering you must add next string to config file:

--[code]--
proxy_buffering off;
--[^code^]--

and restart Nginx daemon.  More information about  configuration you find in documentation on the nginx website.
brudinie at googlemail dot com
14.09.2009 23:21
This flush function avoids error messages related to the buffer not being set when you try to flush.

<?php
function flush (){
    echo(
str_repeat(' ',256));
   
// check that buffer is actually set before flushing
   
if (ob_get_length()){           
        @
ob_flush();
        @
flush();
        @
ob_end_flush();
    }   
    @
ob_start();
}
?>
tobias at dcode dot se
19.06.2009 10:21
Following the speed-up rules from http://developer.yahoo.com/performance/rules.html in the topic "Flush the Buffer Early" with ob_start() will output headers early and thereby corrupt error handling events extended with eg. FirePHP that will trigger something like; "Exception thrown within the exception handler. Message: Headers already sent in on line 0. Cannot send log data to FirePHP." or "Exception thrown without a stack frame in Unknown on line 0".

So use </head><?= ob_flush(); =?><body> instead to invoke this rule/recommendation with ob_start(); to make it work.
jason@jasonbaumgartner@info
17.06.2009 2:18
Many of the suggestions below are very helpful but I would just like to clarify something. 

If you are making calls to a .php script with AJAX (xmlHttpReq) and use flush, this will send data to your script HOWEVER it will not set the xmlHttpReq.readyState to 4 -- which is a requirement to use the information sent (Firefox does however allow you to use the responseText property with a readyState == 3 but IE will throw an error).

To get around this, you will need to make sure of a couple things:

In your Apache php.ini config file, check to make sure that output buffering is disabled:

output_buffering = off

Next, disable gzip compression for the .php script that is called from your AJAX script by using the excellent suggestion from Mandor by placing the following at the top of your script:

@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);

What causes this problem is that while the php child process is running under apache, apache is waiting for the script to complete before completely closing the connection.  Scripts that send information directly to the browser with echos and prints will get away with using flush followed by a sleep command and then clean up procedures.  However, if you notice in the status bar of your browser window, the connection is still held open by apache until the script completed ("Transferring" ... yadda yadda).

By turning off the Apache compression, the connection is terminated directly after a flush while still giving the user the ability to run a sleep command followed by clean up code.

This bit me because the AJAX was returning <img> tags to newly created images by php.  The cleanup script would then remove those images after completion. 

Since Apache held the connection open, the readyState only changed to 4 AFTER my php cleanup fired -- thereby erasing the links referred to my the <img> tags.

With this solution, the readyState will change to 4 BEFORE the sleep command begins.

I hope this saves someone a lot of time and frustration.  AJAX requests are an entirely different beast compared with simple outputting to a browser window.
mandor at mandor dot net
23.12.2008 4:20
This is what I use to turn off pretty much anything that could cause unwanted output buffering and turn on implicit flush:

<?php

   
@apache_setenv('no-gzip', 1);
    @
ini_set('zlib.output_compression', 0);
    @
ini_set('implicit_flush', 1);
    for (
$i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
   
ob_implicit_flush(1);

?>

If it still fails though, keep in mind that Internet Explorer and Safari have a 1k buffer before incremental rendering kicks in, so you'll want to output some padding as well.
Arerano
16.11.2008 15:58
This helped me getting flushing to work.
Using apache with deflate.

Turning compression off for this script: (add it somewhere at the top of the script)
apache_setenv('no-gzip', '1');

However, this only works if php is running as a module rather than a cgi-extension and safe mode must be disabled.

You can also turn the compression off for a directory by making a .htaccess file and adding the following entry:
mod_gzip_on Off

However that affects the whole directory.
Hope I could help.
Seegras
29.10.2008 15:46
- mod_cern_meta will inhibit flush(). No workaround.
- mod_deflate will inhibit flush(). Not sure whether there is a workaround.
- The default-rules of mod_security will inhibit flush() as well. There is a workaround: SecResponseBodyAccess Off
Mr.KTO
12.10.2008 15:14
IE 7, Opera 9.6, any. All is quite simple. Today I just fogot what i do every night, :)

<?php
// All you need is 256 spaces first
echo str_repeat(" ", 256)."<pre>"; flush();

// and ANY TAG before \r\n
echo "working...<br/>\r\n"; flush(); sleep(1); // this in cycle
?>
Lucas
28.08.2008 0:12
I just had some problems with flush() and ob_flush(). What I did to resolve this problem took me some time to figure out so I'd like to share what I came up with.

The main problem is the php setting "output_buffering" which can be set too large and will prevent your text from outputting. To change this value you can either set it in php.ini or you can add the line

php_value output_buffering "0"

to your .htaccess file. It will not work with ini_set() since it is PHP_INI_PERDIR.

Next thing is to begin with ob_start();
Then you need
ob_flush();
flush();
before any echo or print.

Your code might look like this:

<?php
ob_start
();

for(
$i=0;$i<70;$i++)
{
    echo
'printing...<br />';
   
ob_flush();
   
flush();

   
usleep(300000);
}

?>

Hope this helps anyone with the same problems.
David
7.05.2008 23:32
mod_security 2.x core rules will also prevent flush() from working.
3rowley at uppingham dot co dot uk
30.04.2008 20:08
It appears that the flush() command does not work when running under suPHP. I have had it explained to me that this is due to suPHP caching all the PHP output untill the connection has been terminated, thus making this command useless.

Hope that helps someone.
(Also, if anyone comes up with a cunning solution, I would love to hear)

Josh
marques at displague dot com
10.04.2008 19:23
flush() will prevent headers from being sent.

<?php
flush
();
header('Location: testj.php');
?>

Warning: Cannot modify header information - headers already sent in testk.php on line 3
www.koalabear.nl
12.03.2008 13:19
This will work for IE 7.0 :)

Do not use within <PRE> tag because of the extra spaces send

<?php
function hardFlush() {
   
// Like said in PHP description above, some version of IE (7.0 for example)
    // will not 'update' the page if less then 256 bytes are received
    // Send 250 characters extra
   
echo '                                                  ';
    echo
'                                                  ';
    echo
'                                                  ';
    echo
'                                                  ';
    echo
'                                                  ';
   
flush();
   
ob_flush();
}
?>
dale3h
8.11.2007 6:41
When building a progress bar for a time intensive script, I came across a Firefox bug. I have only tested this in Firefox 2.0.0.9.

When using <div> tags for the progress bar, you MUST have a non-whitespace character inside the <div> upon initialization. If you do not, Firefox will freeze until the script is complete.
chuck at artistan dot org
27.06.2007 15:57
had problems flushing tables to screen.
this worked for me

<?php
if (ob_get_level() == 0) ob_start();
loop{
   
tables and stuff
   
echo str_pad("<br>\n",8);
}
ob_flush();
flush();  // needed ob_flush
usleep(50000);// delay minimum of .05 seconds to allow ie to flush to screen
?>
mbilliet at gmail dot com
13.05.2007 12:13
Hello,

I had the same problems with sending a javascript after the <body></body> content has been sent to the browser (the script updates the content of an iframe).

Two solutions work for me:
   - add enough data (i.e.: extra dummy text like spaces) or,
   - echo the '</body>' tag at the end of the page

Note, that for the latter to work one should:
   - turn of output_buffering in php.ini and either:
      * turn on implicit_flush in its php.ini or,
      * call ob_implicit_flush(); at the beginning of a script

I preferr echoing the '</body>' tag at the end as it doesn't require any extra data to be sent over the wire and its much a neater coding technique.

Notes:
   - these worakarounds aren't needed when using perl and cgi,... .
   - you can leave the zlib.output_compression in your php.ini switched on

Kind regards and hoped this is of any help.

Maurits
macott[D0T]daiato[4T]gmail[D0T]com
24.04.2007 9:17
If flush() don't work remember to check if you have any antivirus caching the data sent to the browser.
Leon
30.03.2007 14:14
I've spent days trying to figure out why flush didn't work all of a sudden, while it used to work perfectly. Apparently, it was McAfee Spamkiller that caused problems. Disabling it didn't work, I had to completely remove it. Hope this helps someone.
Kirk
6.02.2007 21:25
If you're not explictly using the buffering functions, then ob_flush() is only necessary if output buffering is turned on in your php.ini file.

flush() is only necessary if implicit_flush is turned off in your php.ini file. Setting implicit_flush to on will remove the need for all these flush() calls, but it's generally only good in an extremely controlled environment. Turning on implicit_flush in a production environment can be bad.
no at spam dot com
3.08.2006 20:51
ob_flush();flush();

Not the other way around, because it wont work.
vlad at modomail dot com
5.04.2006 15: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.11.2005 12: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.11.2005 16: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
3.10.2005 10: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.09.2005 9: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  // not in table tags for IE
echo str_pad('',1024);  // minimum start for Safari
for ($i=10; $i>0; $i--) {
    echo
str_pad("$i<br>\n",8);
   
// tag after text for Safari & Firefox
    // 8 char minimum for Firefox
   
flush();  // worked without ob_flush() for me
   
sleep(1);
}
?>
</body>
</html>
js at jeansebastien dot com
17.07.2005 8: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();

?>
MOELZE �T GMX DOt DE (Michael)
16.05.2005 1: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:

<?php
$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.04.2005 11: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;
   
//This div will show loading percents
   
echo '<div class="percents">' . $i*4 . '%&nbsp;complete</div>';
   
//This div will show progress bar
   
echo '<div class="blocks" style="left: '.$d.'px">&nbsp;</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.04.2005 17: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 the buffer is not set to 0, there's no need to call
// ob_start(), because the buffer is started already.
// You can test that by a call to ob_get_level();. Calling it
// again will cause a second level of buffering to start and
// the script won't work.

if (ob_get_level() == 0) {
   
ob_start();
}
echo
str_pad('Loading... ',4096)."<br />\n";
for (
$i = 0; $i < 25; $i++) {
    echo
'<span class="pbar">&nbsp;</span>';
   
ob_flush();
   
usleep(700000);
}
ob_end_flush();

// Close it out...
?>
</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
15.04.2005 5: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
7.04.2005 13: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.03.2005 19: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
' '; // extra spaces
}
// keeps it flowing to the browser…
flush();
// 50000 microseconds keeps things flowing in safari, IE, firefox, etc
usleep(50000);

?>

This code came from a comment on a blog discussing browser functionality with flush();
crmacd at yahoo dot com
5.03.2005 0: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;'>&nbsp;</td>
<td style='background:#FFCCCC;'>&nbsp;</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
4.03.2005 16: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
1.02.2005 21: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.
Rusty
24.02.2004 18: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
m@rco
29.10.2003 16: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:

<?php
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
1.10.2003 4:40
Regarding Apache2
flush() will produce the same results as ob_flush() when output buffering is involved.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)