我從PHP官方手冊拷貝下來一段關於緩衝的代碼,但是執行結果卻是錯的,求解!

來源:互聯網
上載者:User
我拷貝代碼的地址是:http://fr2.php.net/manual/zh/function.ob-flush.php#109314

My Code如下,執行後不是逐行顯示,而是等了很長時間後,一起出現出來:

header('Content-type:text/html; charset=utf-8');if (ob_get_level() == 0) ob_start();for ($i = 0; $i<10; $i++){    echo "
Line to show."; echo str_pad(' ',4096)."\n"; ob_flush(); flush(); sleep(2);}echo "Done.";ob_end_flush();

OS:Mac osx 10.9
webservice: nginx 1.4
php: 5.6

回複內容:

我拷貝代碼的地址是:http://fr2.php.net/manual/zh/function.ob-flush.php#109314

My Code如下,執行後不是逐行顯示,而是等了很長時間後,一起出現出來:

header('Content-type:text/html; charset=utf-8');if (ob_get_level() == 0) ob_start();for ($i = 0; $i<10; $i++){    echo "
Line to show."; echo str_pad(' ',4096)."\n"; ob_flush(); flush(); sleep(2);}echo "Done.";ob_end_flush();

OS:Mac osx 10.9
webservice: nginx 1.4
php: 5.6

運行毫無問題.
Line to show.
Line to show.
Line to show.
Line to show.
Line to show.
Line to show.
Line to show.
Line to show.
Line to show.
Line to show. Done.

PHP版本太高

輸出下ob_get_level()函數看是不是為0,是0的話應該是沒有開啟輸出緩衝區。我這的ob_get_level()返回1。

注意flushob_flush的區別:
ob_flush 操作的是php本身的buffer
flush 操作的是 apacheflush,也就是說這個對 nginx是無效果的

nginx下要如下設定試一下:

fastcgi_max_temp_file_size  0  #禁止磁碟緩衝fastcgi_buffers 1 1k #把buffer設小

編輯 php.ini,設定 output_buffering=0 禁用 buffering 機制,重啟伺服器試試

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.