ob_flush
(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
ob_flush — 冲刷(发送)活动输出处理程序的返回值
说明
ob_flush(): bool
该函数调用输出处理程序(使用 PHP_OUTPUT_HANDLER_FLUSH
flag),冲刷(发送)其返回值并丢弃活动输出缓冲区的内容。
该函数不会像 ob_end_flush() 或 ob_get_flush() 那样关闭活动输出缓冲区。
如果没有以 PHP_OUTPUT_HANDLER_FLUSHABLE
flag 启动的活动输出缓冲区,ob_flush() 将失败。
参数
此函数没有参数。
错误/异常
如果函数失败会生成 E_NOTICE
。
参见
- ob_start() - 打开输出控制缓冲
- ob_get_contents() - 返回输出缓冲区的内容
- ob_end_flush() - 冲刷(发送)活动输出处理程序的返回值,并关闭活动输出缓冲区
- ob_get_flush() - 冲刷(发送)活动输出处理程序的返回值,返回活动输出缓冲区的内容并将其关闭
- ob_clean() - 清空(擦掉)活动输出缓冲区的内容
+添加备注
用户贡献的备注 7 notes
Lee ¶
12 years ago
As of August 2012, all browsers seem to show an all-or-nothing approach to buffering. In other words, while php is operating, no content can be shown.
In particular this means that the following workarounds listed further down here are ineffective:
1) ob_flush (), flush () in any combination with other output buffering functions;
2) changes to php.ini involving setting output_buffer and/or zlib.output_compression to 0 or Off;
3) setting Apache variables such as "no-gzip" either through apache_setenv () or through entries in .htaccess.
So, until browsers begin to show buffered content again, the tips listed here are moot.
dermeister dot online at gmail dot com ¶
12 years ago
some problems with ob_flush() and flush() could be resolved by defining content type header :
header( 'Content-type: text/html; charset=utf-8' );
so working code looks like this:
<?php
header( 'Content-type: text/html; charset=utf-8' );
echo 'Begin ...<br />';
for( $i = 0 ; $i < 10 ; $i++ )
{
echo $i . '<br />';
flush();
ob_flush();
sleep(1);
}
echo 'End ...<br />';
?>
thecichos at gmail dot com ¶
1 year ago
The output buffer seems to work best when the server is returning a code 206 and setting the output_buffering lower temporarily to let it fill up
This tells the browser to wait for additional content
for example:
// Set the header to 206
header("HTTP/1.1 206 Partial Content; Content-Type: text/html; charset=utf-8");
// Flush the current outputbuffer
flush();
ob_flush();
ob_end_flush();
// Create a new output buffer
ob_start();
// Save the current output buffer size
$tempBuffering = ini_get("output_buffering");
// Set a new, much smaller buffer size
ini_set("output_buffering", 256);
// Do some buffering
!!! All your amazing code goes here !!!
// Fill the buffer with something if needed
echo str_pad(" ", (int)ini_get("output_buffering"), " ");
flush();
ob_flush();
// Revert the buffer size
ini_set("output_buffering", $tempBuffering);
chris - latko - org ¶
9 years ago
Although browsers now have an all or none buffering strategy, the arguments are not moot.
If you are not using ob_flush, you run this risk of exceeding socket timeouts (commonly seen in php-fpm/nginx combos).
Basically, flushing solves the infamous 504 Gateway Time-out error.
Jens ¶
16 years ago
If you call ob_flush() and flush() and still dont get the buffer flushed it might be because some antivirus software (Panda in this case) holds the buffer until the page has finished loaded before sending it to the browser.
jake at qzdesign dot co dot uk ¶
6 years ago
If there is no active output buffer, an error of level E_NOTICE is generated (at least in PHP 7.1). To avoid this, test first with `ob_get_level()`.