2017-04-20 28 views
1

我正在嘗試生成報告並上載它。問題是,當生成文件的函數以readfile()結束時,即使在函數調用之前放置ob_start(),它也會下載文件。所以我不明白爲什麼ob_start沒有趕上它。Ob_start未捕獲輸出文件

問題應該在這裏

  1. 我打電話ob_start()

  2. 然後我打電話功能,輸出文件中像這樣

    header('Content-Type: application/vnd.openxmlformats-officedocument.' . 'wordprocessingml.document'); header('Content-Disposition: attachment; filename="' . $fileNameDownload . '"'); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . filesize($fileName . '.' . $this->_extension)); readfile($fileName . '.' . $this->_extension);

  3. 後以上函數調用我把 $content = ob_get_contents();

和4 ob_end_clen()

我期望與readfile()功能將開始寫入緩衝區,而是它輸出下載文件

+0

向我們展示代碼,否則所有我可以看到是這樣的東西的副本http://stackoverflow.com/questions/10938822/how-do-i-assign-content-from-readfile-into-a-variable – Scuzzy

+1

我編輯了一下,希望這是足夠了,因爲在其他情況下它幾乎沒有任何代碼行。 – jemcaj

+0

如果不回顯緩衝區內容,您是否回顧了腳本執行結束時實際下載的內容?我的想法是你得到一個標題衝突。 http://stackoverflow.com/questions/3111179/how-do-headers-work-with-output-buffering-in-php – Scuzzy

回答

0

我相信一個事實,即頭被設置,並且不會被輸出緩衝區捕獲,您可能需要將其取消,尤其是因爲Content-Disposition: attachment將響應設置爲強制下載。

http://www.php.net/manual/en/function.ob-start.php

該功能將開啓輸出緩衝。當輸出緩衝處於活動狀態時,不會從腳本(標題除外)發送輸出,而是將輸出存儲在內部緩衝區中。

因此,如果函數設置:

header('Content-Type: application/vnd.openxmlformats-officedocument.' . 'wordprocessingml.document'); 
header('Content-Disposition: attachment; filename="' . $fileNameDownload . '"'); 

,你需要做的取消出來你已經結束了輸出緩衝捕獲之後,即:

header('Content-Type: text/html'); 
header('Content-Disposition: inline');