[英]PHP Header Content-Disposition: attachment forces .php file or inline
I want to download a single .mp3 file from my site but when using this code it forces a .php in Firefox and Safari. 我想从我的网站下载单个.mp3文件,但在使用此代码时,它会在Firefox和Safari中强制使用.php。 But in chrome it will send force the file as inline and play on the page. 但是在Chrome中,它会强制将文件作为内联并在页面上播放。 How can i get them to actually download a .mp3 file? 我怎样才能让他们真正下载.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";
}
I have also tried to download a .zip file as well and changing the Content-Type to application/ocetet-stream but it forces .php files on all browsers. 我也尝试下载.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";
}
Try taking the quotes off the filename: 尝试从文件名中取引号:
header('Content-Disposition: attachment; filename=test.mp3');
^^^^^
Getting the name of the script instead of the filename you're trying to use is generally a sign that the filename contains invalid characters for the filesystem the browser's trying to save to. 获取脚本的名称而不是您尝试使用的文件名通常表示文件名包含浏览器尝试保存到的文件系统的无效字符。 eg "
isn't permitted. 例如"
不允许。
I think filesize($track)
is wrong, it should be the whole path filesize("/home/user/gets/".$track)
. 我认为filesize($track)
是错误的,它应该是整个路径filesize("/home/user/gets/".$track)
。 This would cause php to output error messages, preventing you from setting the content-length and disposition header. 这将导致php输出错误消息,阻止您设置内容长度和处置标头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.