[英]PHP forces PDF file download even though I'm using Content-Disposition: inline
[英]PHP Header Content-Disposition: attachment forces .php file or inline
我想從我的網站下載單個.mp3文件,但在使用此代碼時,它會在Firefox和Safari中強制使用.php。 但是在Chrome中,它會強制將文件作為內聯並在頁面上播放。 我怎樣才能讓他們真正下載.mp3文件?
$track = $_SERVER['QUERY_STRING'];
if (file_exists("/home/user/gets/" .$track)) {
header("Content-Type: audio/mpeg");
header('Content-Length: ' . filesize($track));
header('Content-Disposition: attachment; filename="test.mp3"');
$str = "/home/user/gets/".$track;
readfile($str);
exit;
} else {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
echo "no file";
}
我也嘗試下載.zip文件並將Content-Type更改為application / ocetet-stream,但它會強制所有瀏覽器上的.php文件。
//$track = $_SERVER['QUERY_STRING'];
$track = 'testfile.zip';
if (file_exists("/home/user/gets/" .$track)) {
header("Content-Type: application/octet-stream");
header('Content-Length: ' . filesize($track));
header('Content-Disposition: attachment; filename="test.mp3"');
$str = "/home/user/gets/".$track;
readfile($str);
exit;
} else {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
echo "no file";
}
嘗試從文件名中取引號:
header('Content-Disposition: attachment; filename=test.mp3');
^^^^^
獲取腳本的名稱而不是您嘗試使用的文件名通常表示文件名包含瀏覽器嘗試保存到的文件系統的無效字符。 例如"
不允許。
我認為filesize($track)
是錯誤的,它應該是整個路徑filesize("/home/user/gets/".$track)
。 這將導致php輸出錯誤消息,阻止您設置內容長度和處置標頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.