rub*_*bio 3 php mp3 header cross-browser download
我想从我的网站下载单个.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";
}
Run Code Online (Sandbox Code Playgroud)
我也尝试下载.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";
}
Run Code Online (Sandbox Code Playgroud)
尝试从文件名中取引号:
header('Content-Disposition: attachment; filename=test.mp3');
^^^^^
Run Code Online (Sandbox Code Playgroud)
获取脚本的名称而不是您尝试使用的文件名通常表示文件名包含浏览器尝试保存到的文件系统的无效字符.例如"是不允许的.