[英]PHP file download strange error
通過PHP下載文件時出現錯誤。
編碼:
if(file_exists($myFile)) {
$mm_type="application/octet-stream";
header("Cache-Control: public, must-revalidate");
header("Pragma: hack"); // WTF? oh well, it works...
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($myFile)) );
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header("Content-Transfer-Encoding: binary\n");
$fp = fopen($myFile, 'rb');
$buffer = fread($fp, filesize($myFile));
fclose ($fp);
print $buffer;
}
上面的代碼適用於doc文件,pdf文件,zip文件,jpg / png文件,
但是當我嘗試下載.rtf或.txt文件時失敗。
在所有主流瀏覽器(Chrome,FF,IE,Safari)上進行了測試。
還嘗試使用mime-type:沒有運氣的application/force-download
。
錯誤:
我在Chrome中遇到錯誤:
This webpage is not available
在Firefox中:
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
您需要為.rtf和.txt文件添加內容類型
首先檢查文件擴展名,然后在
如果沒有其他條件,則為.rtf和.txt文件設置內容類型
干杯:)
我認為這應該可以幫助您在這里找到所有內容類型
http://www.sitepoint.com/web-foundations/mime-types-complete-list/
這段代碼解決了這個問題:
if (file_exists($myFile)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($myFile));
readfile($myFile);
exit;
}
資料來源: PHP.net
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.