【发布时间】:2012-08-16 21:16:29
【问题描述】:
我想从我的站点下载一个 .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";
}
【问题讨论】:
-
我觉得
filesize($track)不对,应该是整条路径filesize("/home/user/gets/".$track)
标签: php header download cross-browser mp3