PHP错误:ob_flush()[ref.outcontrol]:无法刷新缓冲区.没有要刷新的缓冲区


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

有人能保存这两个文件并运行它们吗?并告诉我为什么会出现错误"ob_flush()[ref.outcontrol]:无法刷新缓冲区。没有缓冲区可刷新"。我试着在谷歌上搜索,结果显示我必须使用ob_start();但当我这样做时,它不会逐行打印出来,而是在完成FOR循环时返回整个对象。我对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>

如果输出缓冲区处于活动状态(例如通过ob_start()或通过配置设置),则仅需要ob_flush()。如果没有,只需移除ob_flush()即可。或者你可以把它设为条件:

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

我认为您混淆了ob_flush()flush()。虽然ob_start()ob_flush()处理捕获所有输出的PHP内部输出缓冲区,但flush()是像其他编程语言一样刷新STDOUT的正常函数。

示例:

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

编辑:

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

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

还请记住,Safari和Internet Explorer有一个内部1K缓冲区。因此,您需要添加1 KB的填充数据(如空格),以便进行渲染。

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

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

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

ob_start()在哪里?

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

一个例子:

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

手动