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

Thr*_*eaT 12 php ajax flush while-loop

有人可以保存这两个文件并运行它们并告诉我为什么我收到错误"ob_flush()[ref.outcontrol]:无法刷新缓冲区.没有缓冲区要刷新".我试着用Google搜索,它说我必须使用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);
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Wri*_*ken 20

需要ob_flush()如果输出缓冲器被激活(例如,通过ob_start(),或由配置设置).如果还没有,只需删除即可ob_flush().或者你可以使它有条件:

 if( ob_get_level() > 0 ) ob_flush();
Run Code Online (Sandbox Code Playgroud)


ibl*_*lue 11

我想你混淆了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.
Run Code Online (Sandbox Code Playgroud)

编辑:

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

@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
Run Code Online (Sandbox Code Playgroud)

另请注意,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>
Run Code Online (Sandbox Code Playgroud)

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

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