PHP Flush/ob_flush not working

前端 未结 7 2202
情深已故
情深已故 2020-11-27 07:03

I\'ve tried several attempts at getting my flush and ob_flush to work. I\'ve tried setting the ini to allow buffering, I\'ve tried using several different functions I found

相关标签:
7条回答
  • 2020-11-27 07:08

    this works now (at least on php 5.5)

    ob_end_flush();
    while(stuff){
      ..stuff...
      echo('yo');
      flush();
    }
    

    no need to sleep or anything else

    0 讨论(0)
  • 2020-11-27 07:12

    I have had the same problem but a user pointed me out in the right direction, I used a "for" loop to solve this browser specific issue:

    for($i = 0; $i < 5000; $i++)
    {
        echo ' ';
    }
    

    Relate to Outputting exec() ping result progressively for more details.

    0 讨论(0)
  • 2020-11-27 07:21

    The idea here is to disable output buffering, not enable it. As its name says, output buffering will save the output to memory and display it at the end of the script, or when explicitly asked for it.

    That being said, you don't have to flush explicitly for every output. Use the following, before displaying any output, and then you won't have to bother flushing every time you echo something:

    ob_implicit_flush(true);
    ob_end_flush();
    

    Per example:

    ob_implicit_flush(true);
    ob_end_flush();
    
    for ($i=0; $i<5; $i++) {
       echo $i.'<br>';
       sleep(1);
    }
    

    Will output, 0 to 4, with each being displayed every second.

    0 讨论(0)
  • 2020-11-27 07:28

    Please note that you may need to disable gzip compression on your webserver (apache or nginx).

    It was my issue.

    0 讨论(0)
  • 2020-11-27 07:29

    This question seems to pop up a lot on a Google search, so I wanted to update it. It's September 2014.....

    @Netcoder 's answer does work, but Chrome will sometimes still output everything all at once.

    To fix this, simply added an ob_flush(), and flush() in the code, it will output after each second.

    Example:

    ob_implicit_flush(true);
    ob_end_flush();
    
    for ($i=0; $i<5; $i++) {
        echo $i.'<br>';
        ob_flush();
        flush();   
        sleep(1);
    }   
    
    0 讨论(0)
  • 2020-11-27 07:30

    I just wanted to write a quick note of what I've observed, now in 2016, of the different approached suggested:

    The above codes offered by netcoder and David work for me in the following browsers:

    • Chrome
    • Opera

    It does not seem to work in Firefox, Safari, or IE 10-11.

    I've also tested the alternative code:

    <?php
    
        if (ob_get_level() == 0) ob_start();
        for ($i = 0; $i<10; $i++){
    
            echo "<br> Line to show.";
            echo str_pad('',4096)."\n";    
    
            ob_flush();
            flush();
            sleep(2);
        }
    
        echo "Done.";
    
        ob_end_flush();
    ?>
    

    Which can be found here: http://php.net/manual/en/function.flush.php#54841

    Which seems to have better current support through all browsers:

    • Chrome
    • Firefox
    • Opera
    • Safari
    • IE 10
    • IE 11

    The working implementations seem to change year to year, so I wanted to offer an update of what I've found myself to work at the moment.

    0 讨论(0)
提交回复
热议问题