我试图通过浏览器强制下载这里是我的代码:
header("Content-Type: application/force-download");
header('Content-type: audio/mp3');
header('Content-Description: File Download');
header('Content-Disposition: attachment; filename=' . $file_name);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-length: ' . filesize($file_path);
ob_clean();
flush();
readfile($file_path);
这在我的本地机器上完美运行,但是当我将它上传到我的实时服务器时,二进制文件被转储到浏览器,那些乱码的字符填满了浏览器窗口。可能是什么问题呢?我在共享服务器上,所以我不知道我的apache配置是否需要更改等等。
我接受了@sanmi的建议并使用Firebug来查看响应标题,这是我得到的:
Here is what I got sanmai:
Server nginx/0.7.67
Date Tue, 06 Sep 2011 15:02:03 GMT
Content-Type text/html; charset=UTF-8
Transfer-Encoding chunked
Connection keep-alive
Expires Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma no-cache
Vary Accept-Encoding,User-Agent
Content-Encoding gzip
我可以看到内容类型条目已更改为text / html,并且服务器实际上是nginx条目。那么这是一个nginx问题还是我的标题条目错了?
谢谢, Tsega
答案 0 :(得分:0)
下面
header("Content-Type: application/force-download");
header('Content-type: audio/mp3');
您发送了两个Content-Type
,只需要一个。
答案 1 :(得分:0)
使用FireBug或other means查看服务器实际发送的HTTP标头。它可能隐藏或改变它们中的任何一个。如果是这样,请与您的托管服务商联系。
答案 2 :(得分:0)
我不确定这是否会导致它,但内容类型音频/ mp3未定义(正式):http://www.iana.org/assignments/media-types/audio/index.html。尝试使用audio / mpeg?
答案 3 :(得分:0)
在关闭php标记之后,我所包含的文件之一变成了一个空行。这就是问题,感谢大家的帮助。