簡體   English   中英

PHP Flush/ob_flush 不起作用

[英]PHP Flush/ob_flush not working

我已經嘗試了幾次讓我的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();

我希望它回顯它正在做什么,然后運行該函數,然后回顯其他內容,然后執行另一個功能。 我希望在瀏覽器上實時刷新和回顯所有緩沖區。

這里的想法是禁用輸出緩沖,而不是啟用它。 顧名思義,輸出緩沖會將輸出保存到內存中,並在腳本末尾或明確要求時顯示。

話雖如此,您不必為每個輸出顯式刷新。 在顯示任何輸出之前使用以下內容,然后每次回顯時都不必費心沖洗:

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,每個每秒顯示一次。

我只是想快速記錄一下我在 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

通過所有瀏覽器似乎有更好的當前支持:

  • 鉻合金
  • 火狐
  • 歌劇
  • 蘋果瀏覽器
  • 瀏覽器 10
  • 瀏覽器 11

工作實現似乎每年都在變化,所以我想提供我目前發現自己工作的更新。

請注意,您可能需要在您的網絡服務器(apache 或 nginx)上禁用 gzip 壓縮。

這是我的問題。

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

這個問題似乎在谷歌搜索中彈出了很多,所以我想更新它。 現在是 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);
}   

我遇到了同樣的問題,但用戶指出了正確的方向,我使用“for”循環來解決此瀏覽器特定問題:

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

有關更多詳細信息,請參閱逐步輸出 exec() ping 結果

這現在有效(至少在 php 5.5 上)

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

無需睡覺或其他任何事情

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM