Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

flush

(PHP 3, PHP 4, PHP 5)

flush -- Flush the output buffer

Description

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



ob_clean> <Output Control
Last updated: Fri, 26 Jan 2007
 
add a note add a note User Contributed Notes
flush
chuck at artistan dot org
27-Jun-2007 06:57
had problems flushing tables to screen.
this worked for me
<?
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-May-2007 03: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-Apr-2007 12:17
If flush() don't work remember to check if you have any antivirus caching the data sent to the browser.
Leon
30-Mar-2007 05: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
06-Feb-2007 12: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.
anonymous
10-Nov-2006 02:42
Also, if you don't have a global php.ini file, PHP will laugh at you for calling flush()... and so will I.
no at spam dot com
03-Aug-2006 11:51
ob_flush();flush();

Not the other way around, because it wont work.
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  // 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>
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
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com
Z058440144362 Z348613067571