12

我已经尝试过几次尝试让我的 flush 和 ob_flush 工作。我尝试将 ini 设置为允许缓冲,我尝试使用我在网上找到的几种不同的功能来进行输出缓冲,但它们都不起作用。该脚本希望等到它完全完成,直到它回显输出。这是我到目前为止的脚本

 ob_start();

 //Login User
 echo 'Logging in to user<br>';
       ob_flush();
       flush();
      $ch = curl_init("http://www.mysite.com/login/");
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$user&pass=$pass");
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies/$cookie");
      curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies/$cookie");
      $output = curl_exec($ch);
      curl_close($ch);
      ob_flush();
      flush();

       //Update Status
 echo 'Updating Status<br>';
       ob_flush();
       flush();
      $ch = curl_init("http://www.mysite.com/update/");
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, "status=$status");
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies/$cookie");
      curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies/$cookie");
      $output = curl_exec($ch);
      curl_close($ch);
      ob_flush();
      flush();

我希望它回显它正在做的事情,然后运行该函数,然后回显其他内容,然后执行另一个功能。我希望在浏览器上实时刷新和回显所有缓冲区。

4

8 回答 8

46

这里的想法是禁用输出缓冲,而不是启用它。顾名思义,输出缓冲会将输出保存到内存中,并在脚本末尾或明确要求时显示。

话虽如此,您不必为每个输出显式刷新。在显示任何输出之前使用以下命令,这样您就不必在每次回显某些内容时都费心冲洗:

ob_implicit_flush(true);
ob_end_flush();

每个例子:

ob_implicit_flush(true);
ob_end_flush();

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

将输出 0 到 4,每一个每秒显示一次。

于 2010-12-19T04:28:03.127 回答
9

我只是想快速记录一下我在 2016 年观察到的不同方法建议:

netcoderDavid提供的上述代码适用于以下浏览器:

  • 铬合金
  • 歌剧

它似乎不适用于 Firefox、Safari 或 IE 10-11。

我还测试了替代代码:

<?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();
?>

可以在这里找到:http: //php.net/manual/en/function.flush.php#54841

这似乎在所有浏览器中都有更好的当前支持:

  • 铬合金
  • 火狐
  • 歌剧
  • 苹果浏览器
  • IE 10
  • 即 11

工作实现似乎每年都在变化,所以我想提供一个我发现自己目前工作的更新。

于 2016-01-25T15:44:32.540 回答
5

请注意,您可能需要在您的网络服务器(apache 或 nginx)上禁用 gzip 压缩。

这是我的问题。

于 2016-09-24T21:25:37.130 回答
3
<?php
    header('Content-Type: text/html; charset=utf-8');

    // I think maybe you can set output_buffering using ini_set here, but I'm not sure.
    // It didn't work for me the first time at least, but now it does sometimes...
    // So I set output_buffering to Off in my php.ini,
    // which normally, on Linux, you can find at the following location: /etc/php5/apache2/php.ini

    @ini_set('output_buffering','Off');
    @ini_set('zlib.output_compression',0);
    @ini_set('implicit_flush',1);
    @ob_end_clean();
    set_time_limit(0);
    ob_start();

    //echo str_repeat('        ',1024*8); //<-- For some reason it now even works without this, in Firefox at least?
?>
<!DOCTYPE html>
<html>
    <head>
        <title>PHP Flushing</title>
    </head>
    <body>
        <h1>Flushing the webpage in real-time using PHP.</h1>
<?php
    ob_flush();
    flush();

    //Note: ob_flush comes first, then you call flush. I did this wrong in one of my own scripts previously.
    for($i=0; $i<5; $i++) {
        echo $i.'<br>';
        ob_flush();
        flush();   
        sleep(1);
    }
?>
    </body>
</html>
于 2016-09-06T13:45:50.610 回答
2

这个问题似乎在谷歌搜索中弹出了很多,所以我想更新它。现在是 2014 年 9 月......

@Netcoder 的答案确实有效,但 Chrome 有时仍会同时输出所有内容。

要解决此问题,只需在代码中添加一个ob_flush(), and flush(),它将在每秒后输出。

例子:

ob_implicit_flush(true);
ob_end_flush();

for ($i=0; $i<5; $i++) {
    echo $i.'<br>';
    ob_flush();
    flush();   
    sleep(1);
}   
于 2014-09-05T02:43:23.503 回答
0

我也遇到了同样的问题,但用户指出了正确的方向,我使用“for”循环来解决这个浏览器特定问题:

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

有关更多详细信息,请参阅逐步输出 exec() ping 结果

于 2012-05-30T11:32:10.553 回答
0

现在可以使用(至少在 php 5.5 上)

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

无需睡觉或其他任何事情

于 2015-07-04T10:44:50.537 回答
0

2021 年更新

我遇到了同样的问题,我基本上使用了 Imanuel Habekotte 的解决方案

在我的 .htaccess 文件中;

SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI .*/my_file_name.php$ no-gzip dont-vary

然后在我的php代码文件(my_file_name.php)中;

header('Content-Type: text/octet-stream; charset=utf-8');
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.


    ini_set('max_execution_time', 0);
    @ini_set('output_buffering','Off');
    @ini_set('zlib.output_compression',0);
    @ini_set('implicit_flush',1);
    @ob_end_clean();
    set_time_limit(0);
    ob_start();
    ob_implicit_flush(true);


/**
    Send a partial message
*/
function send_message($id, $message, $progress) 
{
    $d = array('message' => $message , 'progress' => $progress);
    
    echo json_encode($d) . PHP_EOL;
    
    echo str_pad('',8192)."\n";
    
    //PUSH THE data out by all FORCE POSSIBLE
    ob_flush();
    flush();
}
于 2021-08-11T08:33:59.020 回答