PHP: Hypertext Preprocessor (original) (raw)

flush

(PHP 4, PHP 5, PHP 7, PHP 8)

flush — Flush system output buffer

Description

Note: flush() may not be able to override the buffering scheme of the web server and it has no effect on any client-side buffering in the browser.

Note: This function does not have any effect on user level output handlers such as those started by ob_start() or output_add_rewrite_var().

Warning

flush() can interfere with output handlers that set and send headers in a web context (e.g. ob_gzhandler()) by sending headers before these handlers can do so.

Parameters

This function has no parameters.

Return Values

No value is returned.

Changelog

Version Description
8.4.0 Flushing headers without a body will now succeed in FastCGI.

See Also

Found A Problem?

js at jeansebastien dot com

19 years ago

`This will show each line at a time with a pause of 2 seconds.
(Tested under IEx and Firefox)

i=0;i = 0; i=0;i<10; $i++){ echo "
Line to show."; echo str_pad('',4096)."\n"; ob_flush(); flush(); sleep(2); } echo "Done.";ob_end_flush();?>

`

fran at fran dot cr

4 years ago

`If you want to make flush work when using php-fpm from Apache httpd with mod_proxy_fcgi, since 2.4.31 you can append flushpackets=on to enable flushing, which is instant by default, and and flushwait=n where n is in milliseconds to delay the flushing time for performance seconds.

These values can be appended to the directive, e.g. <Proxy "fcgi://localhost/" flushpackets=on flushwait=500> or in ProxyPass and ProxyPassMatch lines.

The mod_proxy_fcgi documentation for 2.4 does not document this, but it is available in the 2.5 or trunk documentation.

`

Ghostshaw at spymac dot com

20 years ago

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

`

Anonymous

5 years ago

`Regarding buffering by browser you can use: out=strpad(out=str_pad(out=strpad(yourMessage.'
',1024);
print($out);
ob_flush();
flush();

Works for me in Firefox 60 ESR.

`

mandor at mandor dot net

16 years ago

`This is what I use to turn off pretty much anything that could cause unwanted output buffering and turn on implicit flush:

i<obgetlevel();i < ob_get_level(); i<obgetlevel();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.

`

php at stelio dot net

11 years ago

`For a Windows system using IIS, the ResponseBufferLimit takes precedence over PHP's output_buffering settings. So you must also set the ResponseBufferLimit to be something lower than its default value.

For IIS versions older than 7, the setting can be found in the %windir%\System32\inetsrv\fcgiext.ini file (the FastCGI config file). You can set the appropriate line to:
ResponseBufferLimit=0

For IIS 7+, the settings are stored in %windir%\System32\inetsrv\config. Edit the applicationHost.config file and search for PHP_via_FastCGI (assuming that you have installed PHP as a FastCGI module, as per the installation instructions, with the name PHP_via_FastCGI). Within the add tag, place the following setting at the end:
responseBufferLimit="0"
So the entire line will look something like:

Alternatively you can insert the setting using the following command:
%windir%\system32\inetsrv\appcmd.exe set config /section:handlers "/[name='PHP_via_FastCGI'].ResponseBufferLimit:0"

`

no at spam dot com

18 years ago

`ob_flush();flush();

Not the other way around, because it wont work.

`

glafarge

5 years ago

When using PHP with mode CGI/FastCGI there is a new buffer (initiated by mod_fcgid with a default size of 65536 bytes). That causes `flush()` and `ob_flush()` not working as expected with implicit_flush(true). Setting "OutputBufferSize 0" in mod_fcgid config resolves the issue. Hope it helps !

mikael at oebb dot net

19 years ago

`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

`

siggi AT june systems DOT com

20 years ago

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

`

matt at nospamplease dot hevanet dot com

20 years ago

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

i<40000;i < 40000; 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();

`

seb dot field at gmail dot com

14 years ago

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

``

php nospace juju ta ggooggle mail

9 years ago

`combining some ideas i was finally able to get a long running script to give me real time feedback on what it was doing. this was a wamp setup with php running as cgi. i'm pretty sure that apache just wasn't sending any of the buffered output because it was trying to be helpful. also trying to be helpful, i hope this example solution helps someone.

timenow=timenow=timenow=instant["sec"];//Start Time //timeloop(e.g. for security_save after 30 min) while (1) { echo "";//Useless (only to quickload next //or same Site when do a switch) flush(); //giveout buffer instant=gettimeofday();<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>i</mi><mi>m</mi><mi>e</mi><mi>a</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>a</mi><mi>l</mi><mo>=</mo></mrow><annotationencoding="application/x−tex">timeactual=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">im</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal">c</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">l</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>instant["sec"];//getActualTimeinSecs<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>l</mi><mi>a</mi><mi>g</mi><mo>=</mo><mostretchy="false">(</mo><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">flag=((</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">l</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">g</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mopen">((</span></span></span></span>timeactual>instant=gettimeofday(); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>i</mi><mi>m</mi><mi>e</mi><mi>a</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>a</mi><mi>l</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">timeactual=</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">im</span><span class="mord mathnormal">e</span><span class="mord mathnormal">a</span><span class="mord mathnormal">c</span><span class="mord mathnormal">t</span><span class="mord mathnormal">u</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>instant["sec"]; //get Actual Time in Secs <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>l</mi><mi>a</mi><mi>g</mi><mo>=</mo><mo stretchy="false">(</mo><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">flag=((</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">((</span></span></span></span>timeactual>instant=gettimeofday();<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>i</mi><mi>m</mi><mi>e</mi><mi>a</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>a</mi><mi>l</mi><mo>=</mo></mrow><annotationencoding="application/xtex">timeactual=</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">im</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal">c</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="marginright:0.01968em;">l</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>instant["sec"];//getActualTimeinSecs<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>l</mi><mi>a</mi><mi>g</mi><mo>=</mo><mostretchy="false">(</mo><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">flag=((</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;verticalalign:0.1944em;"></span><spanclass="mordmathnormal"style="marginright:0.10764em;">f</span><spanclass="mordmathnormal"style="marginright:0.01968em;">l</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="marginright:0.03588em;">g</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mopen">((</span></span></span></span>timeactual>timenow+$diff)? 1:0);//$diff=switchTime if ($flag) { what_do_at_switch_Time();//Sec.Save etc.etc. timenow=timenow=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...

`

Leon

18 years ago

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

18 years ago

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

`

Arerano

16 years ago

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

`

remco dot pc at outlook dot com

1 year ago

`on php8.3-fpm with apache 2.4.57

In order flush to work in the browser you need to send some more data (at least 4096 bytes / flush) and you need to add a setting for that site to the virtual host of your apache2 configuration:

<FilesMatch .php$>
SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost"

<Proxy fcgi://localhost/">
ProxySet enablereuse=on flushpackets=on

`