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

问题描述

我想你混淆了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的填充数据(如空格)以使其呈现。

您的实现已损坏。您想使用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_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>

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...