I have a loop that takes very long to execute, and I want the script to display something whenever the loop iteration is done.
我有一个循环需要很长时间才能执行,我希望脚本在循环迭代完成时显示一些东西。
echo "Hello!";
flush();
for($i = 0; $i < 10; $i ++) {
echo $i;
//5-10 sec execution time
flush();
}
This does not display the echos until the entire script is completed. What went wrong?
在整个脚本完成之前,这不会显示回声。什么地方出了错?
6 个解决方案
#1
19
From PHP Manual:
来自PHP手册:
flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those.
flush()可能无法覆盖Web服务器的缓冲方案,并且它对浏览器中的任何客户端缓冲没有影响。它也不会影响PHP的用户空间输出缓冲机制。这意味着如果使用ob输出缓冲区,则必须同时调用ob_flush()和flush()来刷新ob输出缓冲区。
echo "Hello!";
flush();
ob_flush();
for($i = 0; $i < 10; $i ++) {
echo $i;
//5-10 sec execution time
flush();
ob_flush();
}
-or- you can flush and turn off Buffering
- 或者 - 您可以刷新并关闭缓冲
<?php
//Flush (send) the output buffer and turn off output buffering
while (ob_get_level() > 0)
ob_end_flush();
echo "Hello!";
for($i = 0; $i < 10; $i ++) {
echo $i . "\r\n";
}
?>
#2
4
try this
while (@ob_end_flush());
ob_implicit_flush(true);
echo "first line visible to the browser";
echo str_pad("",1024," ");
echo "<br />";
sleep(5);
echo "second line visible to the browser after 5 secs";
Just notice that this way you're actually disabling the output buffer for your current script. So if you're trying to 'ob_end_flush()' after that you'll get a warning that there is no buffer to close.
请注意,这样您实际上是禁用当前脚本的输出缓冲区。因此,如果您在此之后尝试'ob_end_flush()',您将收到一条警告,表示没有要关闭的缓冲区。
#3
3
Make sure you first do:
请务必先做:
@ini_set('zlib.output_compression', 0); @ini_set('implicit_flush', 1); @ob_end_clean();
@ini_set('zlib.output_compression',0); @ini_set('implicit_flush',1); @ob_end_clean();
and then just flush();
every time you need to output your echo'es to the browser.
然后只是flush();每次你需要将echo'es输出到浏览器。
#4
1
In general, the desired behavior isn't possible is a deterministic/stable way using pure PHP and HTML.
通常,使用纯PHP和HTML的确定性/稳定方式是不可能的。
If and how a browser renders a partial page depends on the browser, proxies and caches. Thus, even if the stuff works on your test machine, it's likely, that it does not on your production system.
浏览器呈现部分页面的方式和方式取决于浏览器,代理和缓存。因此,即使这些东西在您的测试机器上运行,也可能在您的生产系统上没有。
The library xAjax provides a well-integrated solution to manage AJAX style updates with PHP. While xAjax might be dead as a project (at least right now), it still works fine.
库xAjax提供了一个集成良好的解决方案,可以使用PHP管理AJAX样式更新。虽然xAjax可能已经作为一个项目死了(至少现在),它仍然可以正常工作。
#5
0
You could try to also use ob_flush() sometimes both are needed to work.
您可以尝试使用ob_flush(),有时两者都需要工作。
#6
0
Also make sure to output a Content-type header first. Flushing doesn't work for me without:
还要确保首先输出Content-type标头。没有以下情况,法拉盛对我不起作用:
header( 'Content-type: text/html; charset=utf-8' );
for($i=0; $i<10; ++$i) {
echo "Loop<br />\n";
ob_flush();
flush();
sleep(1);
}
#1
19
From PHP Manual:
来自PHP手册:
flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those.
flush()可能无法覆盖Web服务器的缓冲方案,并且它对浏览器中的任何客户端缓冲没有影响。它也不会影响PHP的用户空间输出缓冲机制。这意味着如果使用ob输出缓冲区,则必须同时调用ob_flush()和flush()来刷新ob输出缓冲区。
echo "Hello!";
flush();
ob_flush();
for($i = 0; $i < 10; $i ++) {
echo $i;
//5-10 sec execution time
flush();
ob_flush();
}
-or- you can flush and turn off Buffering
- 或者 - 您可以刷新并关闭缓冲
<?php
//Flush (send) the output buffer and turn off output buffering
while (ob_get_level() > 0)
ob_end_flush();
echo "Hello!";
for($i = 0; $i < 10; $i ++) {
echo $i . "\r\n";
}
?>
#2
4
try this
while (@ob_end_flush());
ob_implicit_flush(true);
echo "first line visible to the browser";
echo str_pad("",1024," ");
echo "<br />";
sleep(5);
echo "second line visible to the browser after 5 secs";
Just notice that this way you're actually disabling the output buffer for your current script. So if you're trying to 'ob_end_flush()' after that you'll get a warning that there is no buffer to close.
请注意,这样您实际上是禁用当前脚本的输出缓冲区。因此,如果您在此之后尝试'ob_end_flush()',您将收到一条警告,表示没有要关闭的缓冲区。
#3
3
Make sure you first do:
请务必先做:
@ini_set('zlib.output_compression', 0); @ini_set('implicit_flush', 1); @ob_end_clean();
@ini_set('zlib.output_compression',0); @ini_set('implicit_flush',1); @ob_end_clean();
and then just flush();
every time you need to output your echo'es to the browser.
然后只是flush();每次你需要将echo'es输出到浏览器。
#4
1
In general, the desired behavior isn't possible is a deterministic/stable way using pure PHP and HTML.
通常,使用纯PHP和HTML的确定性/稳定方式是不可能的。
If and how a browser renders a partial page depends on the browser, proxies and caches. Thus, even if the stuff works on your test machine, it's likely, that it does not on your production system.
浏览器呈现部分页面的方式和方式取决于浏览器,代理和缓存。因此,即使这些东西在您的测试机器上运行,也可能在您的生产系统上没有。
The library xAjax provides a well-integrated solution to manage AJAX style updates with PHP. While xAjax might be dead as a project (at least right now), it still works fine.
库xAjax提供了一个集成良好的解决方案,可以使用PHP管理AJAX样式更新。虽然xAjax可能已经作为一个项目死了(至少现在),它仍然可以正常工作。
#5
0
You could try to also use ob_flush() sometimes both are needed to work.
您可以尝试使用ob_flush(),有时两者都需要工作。
#6
0
Also make sure to output a Content-type header first. Flushing doesn't work for me without:
还要确保首先输出Content-type标头。没有以下情况,法拉盛对我不起作用:
header( 'Content-type: text/html; charset=utf-8' );
for($i=0; $i<10; ++$i) {
echo "Loop<br />\n";
ob_flush();
flush();
sleep(1);
}