[英]php files to be downloaded displayed in browser instead of downloaded
我試圖使用PHP腳本下載文本文件,但是當腳本運行時,文件內容顯示在瀏覽器窗口中。 我嘗試了以下解決方案: 為什么不下載下載的文件,而是顯示在瀏覽器中? 但是我已經有的代碼具有內容長度標頭。 這是我的PHP代碼(我從另一篇文章1中獲得並針對文本文件進行了修改)
$file = 'file.txt';
header('Content-Description: File Transfer');
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
我試圖從php.com復制一個示例以獲取pdf文件,但得到的符號與我鏈接的帖子中的符號相同。
我不確定為什么會這樣。 我在Chrome上使用PHP版本5.6.10運行MAMP 3.5。
自從到目前為止,我已經能夠在線找到我所做的所有解決方案,這真讓我感到困惑。 據我所知,我在做什么是正確的,因此任何建議都將不勝感激。
非常感謝,
尼爾·帕特爾(Nehal Patel)
在Marcus的指導下解決(9/4/15):刪除了flush()和ob_clean()。
$file = 'file.txt';
header('Content-Description: File Transfer');
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
嘗試將內容類型更改為八位字節/流
header('Content-Type: application/octet-stream');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.