【发布时间】:2011-09-09 22:17:11
【问题描述】:
我试图通过浏览器强制下载这是我的代码:
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 问题还是我的标头条目错误?
谢谢, 世嘉
【问题讨论】:
-
如果你在服务端注释了 ob_clean() 和 flush() ,会有什么反应?
-
做到了,但还是同样的问题。