php 调用 ob_flush() 和 flush(),但浏览器在脚本完成之前不显示任何输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5770917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:26:13  来源:igfitidea点击:

Calling ob_flush() and flush(), yet browser doesn't show any output until script finishes

phpoutput-buffering

提问by DJafari

Hi Please View Below Code :

嗨,请查看以下代码:

<?php
ob_start();

echo "Start ...<br />\n";
for( $i = 0 ; $i < 10 ; $i++ )
{
    echo "$i<br />\n";
    ob_flush(); 
    flush();
    sleep(1);
}
echo "End ...<br />\n";
?>

It's Incorrect ? i'm tested it but my output show when script is done, have any solution ?

不正确?我已经测试过了,但我的输出显示脚本完成后,有什么解决方案吗?

回答by Rahul Shinde

Hey man I was also got stuck in this problem and finally got the correct solution here it is for you

嘿,伙计,我也被这个问题困住了,终于找到了正确的解决方案,它适合你

you have to add content type for your page you can do that by two ways 1. using html tag

你必须为你的页面添加内容类型你可以通过两种方式来做到这一点 1. 使用 html 标签

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

Ex.

前任。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Wp Migration</title>
</head>
<body>
<?php 
for($i=0;$i<70;$i++)
{
echo 'printing...<br>';
ob_flush();
flush();
sleep(3);
}
?>
</body>
</html>
  1. using php header function

    <?php header( 'Content-type: text/html; charset=utf-8' ); ?>

  1. 使用php头函数

    <?php header( 'Content-type: text/html; charset=utf-8' ); ?>

Ex.

前任。

<?php 
header( 'Content-type: text/html; charset=utf-8' );
for($i=0;$i<70;$i++)
{
echo 'printing...<br>';
ob_flush();
flush();
sleep(3);
}
?>

All the best

祝一切顺利

回答by Jürgen Thelen

Some browsers need to receive at least 256 characters before they start to render. Have you already tried to stuff more output like:

一些浏览器在开始渲染之前需要接收至少 256 个字符。您是否已经尝试填充更多输出,例如:

echo str_repeat('&nbsp;', 50) . "$i<br />\n";

EDIT:

编辑

Under Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i PHP/5.2.9I was able to reproduce the problem of the OP by setting

Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i PHP/5.2.9我能够通过设置重现OP的问题

zlib.output_compression = On

Turning it off again by

再次关闭它

zlib.output_compression = Off

made the script work as wanted.

使脚本按需要工作。

回答by Pascal MARTIN

Try removing the call to ob_start()on your first line : there is no need for you to enable output buffering -- and it probably causes troubles, here.

尝试ob_start()在您的第一行中删除对 的调用:您无需启用输出缓冲——这可能会导致麻烦,在这里。


I've tested your code :


我已经测试了你的代码:

  • If ob_start()is called on the first line, I only see the output when the script finishes, after 10 seconds
  • If I remove that call to ob_start(), then, I see one line of output every second, as soon as it's displayed on the standard output.
  • 如果ob_start()在第一行调用,我只会在脚本完成时看到输出,10 秒后
  • 如果我删除对 的调用ob_start(),那么,一旦它显示在标准输出上,我就会每秒看到一行输出。

回答by Matthias Noback

Using Chrome, I found out that many more bytes are required to by-pass the browser's buffer. In my case 4096 bytes was fine:

使用 Chrome,我发现绕过浏览器的缓冲区需要更多的字节。在我的情况下,4096 字节就可以了:

echo str_repeat(' ', 4096);

Also, adding some HTML element at the beginning also seemed to be mandatory:

此外,在开头添加一些 HTML 元素似乎也是强制性的:

echo $content . '<br />';

回答by Ulix

On my system it appears that FF4 needs more than 256 bytes to start rendering what is arriving from the server side, then i resolved with this at the beginning:

在我的系统上,FF4 似乎需要超过 256 个字节才能开始呈现来自服务器端的内容,然后我在开始时解决了这个问题:

while (@ob_end_flush());
echo(str_repeat(' ',1024));
// ...etc...

回答by Josh Harrison

I've discovered that this was due to Apache's gzip compression being in use for my case.

我发现这是由于我的案例使用了 Apache 的 gzip 压缩。

To turn gzip off for the 'flushing' script only, I created a new .htaccessfile in the directory where the continuous output script resides, with the following:

为了仅为“刷新”脚本关闭 gzip,我在.htaccess连续输出脚本所在的目录中创建了一个新文件,内容如下:

<IfModule mod_env.c>
    SetEnv no-gzip 1
</IfModule>

Flushing is working as expected again.

冲洗再次按预期工作。

回答by Alex S

For people using FCGI / fast cgi.

对于使用 FCGI / 快速 cgi 的人。

FcgidOutputBufferSize 0

回答by Dylan B

You need to add a .htaccessfile to disable gzip output

您需要添加一个.htaccess文件来禁用 gzip 输出

<IfModule mod_env.c>
    SetEnv no-gzip 1
</IfModule>

回答by Ahmet U?ur

I am using laravel framework and buffering did not work but. This is solution :

我正在使用 laravel 框架,但缓冲不起作用。这是解决方案:

header( 'Content-type: text/html; charset=utf-8' );
ob_start();

ob_end_flush();
ob_flush();
flush();
for($i = 1;$i<= 5;$i++){
    echo $i;
    ob_flush();
    flush();

    sleep(3);
}

You have to use first ob_end_flush();

你必须先使用 ob_end_flush();

回答by Deepak Thomas

This flow works with Laravel too

这个流程也适用于 Laravel

ob_implicit_flush(true);
echo "Processing ... "; // Or give out JSON output
ob_flush();
sleep(5); //A time-consuming synchronous process (SMTP mail, maybe?)
echo "Done";