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

PHP错误:ob_flush()[ref.outcontrol]:未能刷新缓冲区。没有要刷新的缓冲区,php,ajax,while-loop,flush,Php,Ajax,While Loop,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

请有人保存这两个文件并运行它们,告诉我为什么出现错误“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);
//此脚本将1到100之间的数字写入文件
//并不断向用户发送信息
$fp=fopen('/tmp/output.txt',w')或die('Failed to open');
设置时间限制(120);
忽略用户中止(true);
对于($i=0;$i<100;$i++){
echo“parent.document.getElementById('foo').innerHTML+='Line$i
';”; 回声重复序列(“”,2048); 冲洗(); ob_flush(); 睡眠(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>

.msg{background:#aaa;padding:.2em;边框底部:1px#000 solid}
.new{背景色:#3B9957;}
.错误{背景色:#992E36;}

函数助手(){ document.getElementById('loadarea').src='test_process.php'; } 函数kill(){ document.getElementById('loadarea').src=''; }
ob_start()在哪里

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

例如:

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

如果输出缓冲区处于活动状态(例如通过
ob\u start()
或通过配置设置),则您只需要
ob\u flush()
。如果没有,只需删除
ob\u flush()
。或者您可以将其设置为有条件的:

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

我想你把
ob\u flush()
flush()
搞混了。虽然
ob\u start()
ob\u 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.
请记住,Safari和Internet Explorer有一个内部1K缓冲区。因此,您需要添加1KB的填充数据(如空格),以使其呈现

编辑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>

检查php.ini中关于输出缓冲区的配置,它可以自动启动。检查是否启用的另一种方法是在开始时使用ob_end_flush()并删除所有刷新。您还可以使用
phpinfo()进行检查ob_flush
对您没有任何好处),如果您需要它,您不应该使用HTTP,而应该使用其他直接套接字连接。但是由于某些原因,当ob_flush()存在时,它会工作,但它也会抛出该错误,因此,如果只有当错误显示时,或者当
display\u errors
关闭时,错误才会消失,这将是完美的?因为如果它是第一个,很可能只是触发其他地方刷新的非空内容的数量。当我关闭display_errors时,它会再次返回整个对象。谢谢你的回答。我尝试了你的建议,但出于某种原因,它不会逐行返回,而是在完成处理后才返回整个for循环结果。然后以干净的方式实现它。创建每秒轮询服务器的JavaScript和返回文件行数的PHP脚本。然后在客户端显示它。好的,关于AJAX实现,请参见编辑2。它每5秒投票一次。为了更频繁地进行轮询,只需在
setTimeout
中减少数字,我尝试了它,但在while或for循环中它没有刷新,它只是返回循环的处理结果,而不是每次循环时返回的结果。1K缓冲区是我在PHP中实现EventSource循环时遇到的神秘问题。我可以通过添加
echo str\u repeat(“,1024),“\n”来克服它到循环的开始,突然,一切都开始了!
<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>
<?php
$file = explode("\n", file_get_contents("/tmp/output.txt"));
$last_line = $file[count($file)-1];
echo $last_line."%";