简体   繁体   English

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

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

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 ? 我测试了它,但我的输出显示脚本完成后,有任何解决方案吗?

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头功能

    <?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 祝一切顺利

Some browsers need to receive at least 256 characters before they start to render. 某些浏览器在开始渲染之前需要至少接收256个字符。 Have you already tried to stuff more output like: 您是否已尝试填充更多输出:

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.9 I 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. 使脚本按需运行。

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 如果在第一行调用ob_start() ,我只在脚本完成后看到输出,10秒后
  • 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()调用,那么,只要它在标准输出上显示,我就会每秒看到一行输出。

Using Chrome, I found out that many more bytes are required to by-pass the browser's buffer. 使用Chrome,我发现需要更多的字节来绕过浏览器的缓冲区。 In my case 4096 bytes was fine: 在我的情况下4096字节很好:

echo str_repeat(' ', 4096);

Also, adding some HTML element at the beginning also seemed to be mandatory: 此外,在开头添加一些HTML元素似乎也是强制性的:

echo $content . '<br />';

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 .htaccess file 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. 法拉盛再次按预期工作。

对于使用FCGI / fast cgi的人。

FcgidOutputBufferSize 0

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...

One sneaky issue with IE8 and flush(); IE8和flush()的一个偷偷摸摸的问题; is that if you're "flushing" out rows in a table. 如果你正在“刷新”表中的行。 IE will only display tables when they're complete. IE只会在完成后显示表格。 This was my issue, and changing containers from table rows to divs solved the problem. 这是我的问题,将容器从表行更改为div可以解决问题。

You need to add a .htaccess file to disable gzip output 您需要添加.htaccess文件以禁用gzip输出

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

I am using laravel framework and buffering did not work but. 我使用laravel框架和缓冲不起作用但。 This is solution : 这是解决方案:

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();

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";

It is correct. 它是正确的。 Works fine for me from CLI running PHP 5.3.3. 从运行PHP 5.3.3的CLI可以正常工作。 If it's not working for you, your PHP install may have output buffering disabled. 如果它不适合您,您的PHP安装可能会禁用输出缓冲。

I would also suggest putting ob_end_flush() at the end of your script to close the output buffer. 我还建议在脚本末尾放置ob_end_flush()来关闭输出缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM