gpt4 book ai didi

PHP Header Content-Disposition : attachment forces . php 文件或内联

转载 作者:可可西里 更新时间:2023-11-01 00:00:46 24 4
gpt4 key购买 nike

我想从我的网站下载一个 .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');
^^^^^

获取脚本的名称而不是您尝试使用的文件名通常表明文件名包含浏览器尝试保存到的文件系统的无效字符。例如" 是不允许的。

关于PHP Header Content-Disposition : attachment forces . php 文件或内联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11995562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com