Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

解決 PHP readfile 下載大檔案時卡住的問題

編輯:關於 Android 開發

解決 PHP readfile 下載大檔案時卡住的問題

網路上關於這個問題的解法很多,但大多並不夠完善。其實最關鍵的一行是 ob_end_clean()——它會清空輸出緩衝區並關閉緩衝機制,讓檔案得以順利、完整地輸出。

  1. @set_time_limit(0);
  2. ini_set('memory_limit', '16M');
  3. ob_end_clean();
  4. @readfile( $path );

以下引自 PHP 官方手冊對 ob_end_clean 的說明:

This function discards the contents of the topmost output buffer and turns off this output buffering. If you want to further process the buffer...

核心觀念是:關閉輸出緩衝後,readfile() 便能把大檔案直接串流送往客戶端,而不必將整份內容一次載入記憶體,自然就不容易卡住。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved