简体   繁体   English

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

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

相关问题 即使我使用Content-Disposition:inline,PHP也会强制下载PDF文件 - PHP forces PDF file download even though I'm using Content-Disposition: inline 使用 PHP 中的“content-disposition:attachment”标头获取请求的内容 - Get content of a request with 'content-disposition: attachment' header in Php PHP标题:内容处理:附件,然后位置:一些网址 - PHP Header : Content-disposition: attachment , then Location: some url php标头内容处置 - php header Content-disposition 使用Content-Disposition:附件的PHP中的文件下载问题 - File download issue in PHP with Content-Disposition: attachment 如何通过Content-Disposition阅读xls文件返回:PHP附件 - How to read xls file return by Content-Disposition: attachment with PHP PHP Content-Disposition header:文件名中的特殊字符 - PHP Content-Disposition header: Specialcharacters in the filename 通过PHP的标题下载文件后,将document.readyState的状态更改为“完成”(“ Content-Disposition:附件…” - changing state of document.readyState to “complete” after downloading file via PHP's header('Content-Disposition: attachment…' PHP内容处置:附件/内容类型 - PHP Content-Disposition: attachment / Content-Type PHP将S3内容处置设置为附件 - php set s3 content-disposition as attachment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM