PHP Flush 有效......即使在 Nginx 中 [英] PHP Flush that works... even in Nginx
问题描述
是否可以在每次执行循环时回显?例如:
Is it possible to echo each time the loop is executed? For example:
foreach(range(1,9) as $n){
echo $n."
";
sleep(1);
}
不是在循环完成时打印所有内容,而是希望看到它每次打印每个结果.
Instead of printing everything when the loop is finished, I'd like to see it printing each result per time.
推荐答案
FINAL SOLUTION
FINAL SOLUTION
这就是我发现的:
Flush 在 Apache 的 mod_gzip 或 Nginx 的 gzip 下不起作用,因为从逻辑上讲,它是对内容进行 gzip 压缩,为此它必须缓冲内容以对其进行 gzip.任何类型的 Web 服务器 gzipping 都会影响这一点.简而言之,在服务器端,我们需要禁用 gzip 并减小 fastcgi 缓冲区大小.所以:
Flush would not work under Apache's mod_gzip or Nginx's gzip because, logically, it is gzipping the content, and to do that it must buffer content to gzip it. Any sort of web server gzipping would affect this. In short, at the server side, we need to disable gzip and decrease the fastcgi buffer size. So:
在 php.ini 中:
In php.ini:
.output_buffering = 关闭
. output_buffering = Off
.zlib.output_compression = 关闭
. zlib.output_compression = Off
在 nginx.conf 中:
In nginx.conf:
.gzip 关闭;
.proxy_buffering off;
. proxy_buffering off;
手头也有这些行,特别是如果您没有访问 php.ini 的权限:
Also have this lines at hand, specially if you don't have acces to php.ini:
@ini_set('zlib.output_compression',0);
@ini_set('zlib.output_compression',0);
@ini_set('implicit_flush',1);
@ini_set('implicit_flush',1);
@ob_end_clean();
@ob_end_clean();
set_time_limit(0);
set_time_limit(0);
最后,如果你有的话,评论下面的代码:
Last, if you have it, coment the code bellow:
ob_start('ob_gzhandler');
ob_start('ob_gzhandler');
ob_flush();
ob_flush();
PHP 测试代码:
ob_implicit_flush(1);
for($i=0; $i<10; $i++){
echo $i;
//this is for the buffer achieve the minimum size in order to flush data
echo str_repeat(' ',1024*64);
sleep(1);
}
相关:
这篇关于PHP Flush 有效......即使在 Nginx 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!