繁体   English   中英

PHP标头内容 - 处置:附件强制.php文件或内联

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM