PHP的错误:使用ob_flush()[ref.outcontrol]:未能刷新缓冲区。没有缓冲刷新 [英] PHP Error: ob_flush() [ref.outcontrol]: failed to flush buffer. No buffer to flush

查看:2084
本文介绍了PHP的错误:使用ob_flush()[ref.outcontrol]:未能刷新缓冲区。没有缓冲刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有人请保存这些2文件,并运行他们,并告诉我,为什么我得到的错误使用ob_flush()[ref.outcontrol]:未能刷新缓冲区没有缓冲刷新。我试着在Google上搜寻周围,它说,我必须使用ob_start()函数;但是当我做那么它不会打印出一行行,而是返回整个对象从FOR循环的时候,它已经完成。我有点新的PHP的,所以我不知道还有什么地方看。

test_process.php

  //这个脚本会写数字从1到100到文件
//并将持续信息用户
$计划生育=的fopen('/tmp/output.txt','W')或死亡(无法打开);
或者set_time_limit(120);
ignore_user_abort(真正的);

为($ i = 0; $ I< 100; $ I ++){
    回声<脚本类型= \文/ JavaScript的\> parent.document.getElementById('富')的innerHTML + ='行$ I< BR />';< / SCRIPT>中;
    回声str_repeat('',2048);
    冲洗();
    使用ob_flush();
    睡眠(1);
    FWRITE($计划生育,$我的\ n);
}

fclose函数($ FP);
 

main.html中

 < HTML>
    < HEAD>
        <脚本SRC =HTTP://$c$c.jquery.com/jquery-latest.min.js类型=文/ JavaScript的字符集=utf-8>< / SCRIPT>

        <风格类型=文本/ CSS媒体=屏幕>
            。味精{背景:#aaa;填充:.2em;下边框:1px的#000固体}
            。新{背景色:#3B9957;}
            .error {背景色:#992E36;}
        < /风格>

    < /头>
    <身体GT;

        < IFRAME ID =loadareaWIDTH =1024px高度=768px>< / IFRAME>< BR />
        <脚本>
            功能帮手(){
                。的document.getElementById('loadarea')SRC ='test_process.php';
            }
            功能杀死(){
                的document.getElementById('loadarea')SRC =''。
            }
        < / SCRIPT>

        <输入类型=按钮的onclick =帮手()值=开始>
        <输入类型=按钮的onclick =杀了()值=停止>
        < D​​IV ID =富>< / DIV>


< /身体GT;
< / HTML>
 

解决方案

我想你混淆了使用ob_flush()的flush()。虽然 ob_start()函数使用ob_flush()处理映入所有输出一个PHP内部输出缓冲器,的flush()的正常功能是刷新 STDOUT 像在其他编程语言。

例如:

 < PHP
ob_start()函数;
回声Foobar的\ nFoobar \ nFoobar \ N的;
//还没开始打印
使用ob_flush(); //现在它打印出来。

回声Foobar的\ N的; //直接打印,因为包含结束的行。

回声Foobar的; //不打印,因为通常缓冲被刷新到行尾
冲洗(); //打印。
 

编辑:

您输出没有打印出来,因为您的Web服务器可以缓冲的内容。尝试关闭COM pression和输出缓冲:

  @apache_setenv(无gzip压缩',1);
@ini_set('zlib.output_com pression',0);
@ini_set('implicit_flush',1);
 

请也请记住,Safari浏览器和Internet Explorer有内置1K缓冲区。所以,你需要添加1 KB的填充数据(如空格)中,使它们呈现。

编辑2: 你的实现是坏了。你想使用Ajax查询您的数据。使用jQuery在客户端:

 < D​​IV ID =计数器大于0%< / DIV>
<脚本类型=文/ JavaScript的SRC =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js>
<脚本类型=文/ JavaScript的>
功能doPoll(){
    .post的$('脚本的-回报-stuff.php',功能(数据){
        $(#柜台)HTML(数据);
        的setTimeout(doPoll,5000);
    });
}
doPoll();
< / SCRIPT>
 

然后在脚本的-回报-stuff.php

 < PHP
$文件=爆炸(\ N的file_get_contents(/ tmp目录/ output.txt的));
$ last_line = $文件[计数($档案)-1];
回声$ last_line%。
 

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

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>

解决方案

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 STDOUT like in other programming languages.

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.

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

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

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

这篇关于PHP的错误:使用ob_flush()[ref.outcontrol]:未能刷新缓冲区。没有缓冲刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆