PHP 错误:ob_flush() [ref.outcontrol]:未能刷新缓冲区。没有缓冲区可以刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9182094/
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-26 06:18:55  来源:igfitidea点击:

PHP Error: ob_flush() [ref.outcontrol]: failed to flush buffer. No buffer to flush

phpajaxwhile-loopflush

提问by ThreaT

Could someone please save these 2 files and run them and tell me why I get the error " ob_flush() [ref.outcontrol]: failed to flush buffer. No buffer to flush". I tried googling around and it says that I have to use ob_start(); but when I do then it doesn't print out line by line, but rather returns the whole object from the FOR loop when it has completed. I'm kinda new to PHP so I'm not sure where else to look..

有人可以保存这 2 个文件并运行它们并告诉我为什么我收到错误“ob_flush() [ref.outcontrol]: failed to refresh buffer. No buffer to refresh”。我试着用谷歌搜索,它说我必须使用 ob_start(); 但是当我这样做时,它不会逐行打印出来,而是在完成后从 FOR 循环中返回整个对象。我对 PHP 有点陌生,所以我不知道还能在哪里看..

test_process.php

test_process.php

// This script will write numbers from 1 to 100 into file
// And sends continuously info to user
$fp = fopen( '/tmp/output.txt', 'w') or die('Failed to open');
set_time_limit( 120);
ignore_user_abort(true);

for( $i = 0; $i < 100; $i++){
    echo "<script type=\"text/javascript\">parent.document.getElementById( 'foo').innerHTML += 'Line $i<br />';</script>";
    echo str_repeat( ' ', 2048);
    flush();
    ob_flush();
    sleep(1);
    fwrite( $fp, "$i\n");
}

fclose( $fp);

main.html

主文件

<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript" charset="utf-8"></script>

        <style type="text/css" media="screen">
            .msg{ background:#aaa;padding:.2em; border-bottom:1px #000 solid}
            .new{ background-color:#3B9957;}
            .error{ background-color:#992E36;}
        </style>

    </head>
    <body>

        <iframe id="loadarea" width="1024px" height="768px"></iframe><br />
        <script>
            function helper() {
                document.getElementById('loadarea').src = 'test_process.php';
            }
            function kill() {
                document.getElementById('loadarea').src = '';
            }
        </script>

        <input type="button" onclick="helper()" value="Start">
        <input type="button" onclick="kill()" value="Stop">
        <div id="foo"></div>


</body>
</html>

回答by Wrikken

You onlyneed ob_flush()if an output buffer is active (for example by ob_start(), or by configuration settings). If you haven't, just remove the ob_flush(). Or you can make it conditional:

需要ob_flush()如果输出缓冲器被激活(例如,通过ob_start(),或由配置设置)。如果还没有,只需删除ob_flush(). 或者您可以将其设置为有条件的:

 if( ob_get_level() > 0 ) ob_flush();

回答by iblue

I think you are confusing ob_flush()with flush(). While ob_start()and ob_flush()handles a PHP internal output buffer that catches all outputs, flush()is the normal function that flushes STDOUTlike in other programming languages.

我认为您ob_flush()flush(). While ob_start()andob_flush()处理一个捕获所有输出的 PHP 内部输出缓冲区,flush()STDOUT像其他编程语言一样刷新的正常函数。

Example:

例子:

<?php
ob_start();
echo "Foobar\nFoobar\nFoobar\n";
// Nothing printed yet
ob_flush(); // Now it is printed.

echo "Foobar\n"; // Printed directly, because contains a line ending.

echo "Foobar"; // Not printed, because normally buffers are flushed on line endings
flush();  // Printed.

EDIT:

编辑:

Your output is not printed, because your webserver may buffer the contents. Try to turn off compression and output buffering:

您的输出不会打印,因为您的网络服务器可能会缓冲内容。尝试关闭压缩和输出缓冲:

@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);

Please also keep in mind, that Safari and Internet Explorer have an internal 1K buffer. So you need to add 1 KB of padding data (like spaces), to make them render.

还请记住,Safari 和 Internet Explorer 有一个内部 1K 缓冲区。因此您需要添加 1 KB 的填充数据(如空格),以使它们呈现。

EDIT 2:Your implementation is broken. You want to poll your data with ajax. Use jQuery on the client side:

编辑 2:您的实现已损坏。您想使用 ajax 轮询您的数据。在客户端使用 jQuery:

<div id="counter">0%</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
<script type="text/javascript">
function doPoll(){
    $.post('script-that-returns-stuff.php', function(data) {
        $("#counter").html(data);
        setTimeout(doPoll,5000);
    });
}
doPoll();
</script>

Then in script-that-returns-stuff.php:

然后在script-that-returns-stuff.php

<?php
$file = explode("\n", file_get_contents("/tmp/output.txt"));
$last_line = $file[count($file)-1];
echo $last_line."%";

回答by Iznogood

Where is ob_start()?

ob_start() 在哪里?

ob_flush flushes the output buffer to your file handle. Maybe you have it wrong.

ob_flush 将输出缓冲区刷新到您的文件句柄。也许你错了。

An example:

一个例子:

ob_start(); //start output buffering
echo 'hello world'; //not outputed
ob_flush(); //sends the output buffer so displays hello world.

manual

手动的