PHP readfile() 将二进制数据转储到浏览器时出现问题

发布于 2024-12-03 06:43:36 字数 1171 浏览 2 评论 0原文

我试图通过浏览器强制下载,这是我的代码:

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 问题还是我的标头条目错误?

谢谢, 采加

I trying to force a download by the browser here is my code:

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);

This works perfectly on my local machine, but when I upload it to my live server, the binary file is dumped to the browser and those gibberish characters fill up the browser window. What could be the problem? I am on a shared server so I don't know if my apache configuration needs to be changed or something.

I took @sanmi advice and used Firebug to see the response header and here is what I got:

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

I can see that the content-type entry has changed to text/html, and that the server is actually a nginx one. So is this a nginx issue or are my header entries wrong?

Thanks,
Tsega

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初心 2024-12-10 06:43:36

结果我包含的文件之一在结束 php 标记后有一个空行。原来是这个问题,谢谢大家的帮助。

It turns our one of the files I include had a blank line after the closing php tag. That was the problem, thanks everyone for your help.

情独悲 2024-12-10 06:43:36

这里

header("Content-Type: application/force-download"); 
header('Content-type: audio/mp3');

你发送两个Content-Type,只需要一个。

Here

header("Content-Type: application/force-download"); 
header('Content-type: audio/mp3');

You send two Content-Type, only one is necessary.

蓝色星空 2024-12-10 06:43:36

我不确定这是否是造成这种情况的原因,但内容类型audio/mp3未定义(官方): html" rel="nofollow">http://www.iana.org/assignments/media-types/audio/index.html 。尝试使用音频/mpeg?

I am not sure if this might be causing it, but content type audio/mp3 isn't defined(officially): http://www.iana.org/assignments/media-types/audio/index.html . Try using audio/mpeg?

孤独患者 2024-12-10 06:43:36

使用 FireBug其他方式查看服务器实际发送的 HTTP 标头。它可能隐藏或改变其中任何一个。如果是这样,请与您的主机支持人员联系。

Use FireBug or other means to view HTTP headers your server actually sending. It may hide or alter any of them. If so, talk to your hosting's support.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文