30

07-2012

flush
(PHP 3, PHP 4, PHP 5)

flush — 刷新输出缓冲
说明
void flush ( void )


刷新PHP程序的缓冲,而不论PHP执行在何种情况下(CGI ,web服务器等等)。该函数将当前为止程序的所有输出发送到用户的浏览器。

flush() 函数不会对服务器或客户端浏览器的缓存模式产生影响。因此,必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。

个别web服务器程序,特别是Win32下的web服务器程序,在发送结果到浏览器之前,仍然会缓存脚本的输出,直到程序结束为止。

有些Apache的模块,比如mod_gzip,可能自己进行输出缓存,这将导致flush()函数产生的结果不会立即被发送到客户端浏览器。

甚至浏览器也会在显示之前,缓存接收到的内容。例如 Netscape 浏览器会在接受到换行或 html 标记的开头之前缓存内容,并且在接受到 </table> 标记之前,不会显示出整个表格。

一些版本的 Microsoft Internet Explorer 只有当接受到的256个字节以后才开始显示该页面,所以必须发送一些额外的空格来让这些浏览器显示页面内容。
ob_flush
(PHP 4 >= 4.2.0, PHP 5)

ob_flush — Flush (send) the output buffer
Description
void ob_flush ( void )
This function will send the contents of the output buffer (if any). If you want to further process the buffer’s contents you have to call ob_get_contents() before ob_flush() as the buffer contents are discarded after ob_flush() is called.

This function does not destroy the output buffer like ob_end_flush() does.
flush和ob_flush的使用上有一些特别容易犯错的地方,造成无法刷新输出缓冲。
一. flush和ob_flush的正确顺序,正确应是,先ob_flush再flush,如下:
ob_flush();
flush();
如果Web服务器的操作系统是windows系统,那顺序颠倒或者不使用ob_flush()也不会出现问题。但是在Linux系统上就无法刷新输出缓冲。

二. 使用ob_flush()前,确保前面的内容大小足够4069字符。
一些Web服务器的output_buffering默认是4069字符或者更大,即输出内容必须达到4069字符服务器才会flush刷新输出缓冲,为了确保flush有效,最好在ob_flush()函数前有以下语句:
print str_repeat(” “, 4096);
以确保到达output_buffering值。

for ($i=10; $i>0; $i–)
{
echo $i.’<br />’;
ob_flush();
flush();
sleep(1);
}
ob_end_flush();

这个用作类似的进度条,但是如果时间超过30秒,最好还是用set_time_limit(0);进行一下设置。

程序本天成,妙手偶得之!我们只是代码的搬运工!

转载请注明:http://www.521php.com/archives/268/

发表评论

昵称:

网址:

eg.博客主题调用的是Gravatar头像,你可以通过邮箱注册获得头像.
/ 快捷键:Ctrl+Enter