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

后端 未结 3 1414
难免孤独
难免孤独 2021-01-04 09:40

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 go

相关标签:
3条回答
  • 2021-01-04 09:55

    You only need ob_flush() if an output buffer is active (for example by ob_start(), or by configuration settings). If you haven't, just remove the ob_flush(). Or you can make it conditional:

     if( ob_get_level() > 0 ) ob_flush();
    
    0 讨论(0)
  • 2021-01-04 10:02

    Where is ob_start()?

    ob_flush flushes the output buffer to your file handle. Maybe you have it wrong.

    An example:

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

    manual

    0 讨论(0)
  • 2021-01-04 10:06

    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."%";
    
    0 讨论(0)
提交回复
热议问题