2012-08-07 51 views
0

这是我的问题。我正在尝试使用标题下载文件。这里是我的代码:PHP强制下载不起作用

$content_type = mime_content_type('uploads/MyBBIntegrator_v1.3.1.zip'); 
$file = 'uploads/MyBBIntegrator_v1.3.1.zip'; 
header("Cache-Control: public"); 
header('Content-type: application/octet-stream'); 
header("Content-Description: File Transfer"); 
header('Content-Disposition: attachment; filename="MyBBIntegrator_v1.3.1.zip'); 
header("Content-Transfer-Encoding: binary"); 
header('Expires: 0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
readfile('uploads/MyBBIntegrator_v1.3.1.zip'); 

然而,这样做的唯一结果是,页面显示该文件的内容(这是一个文本文件)或奇怪符号的字符串,如果该文件是图像/ ZIP/exe等

我该怎么做才能解决这个问题?

+1

删除内容长度标题并尝试.. – 2012-08-07 11:29:59

+0

它为我工作?哪个浏览器? – 2012-08-07 13:10:34

+0

作为开始,删除所有不需要或未定义的响应头字段(Content-Description,Content-Transfer-Encoding,Cache-Control:public ...) – 2012-08-13 08:17:36

回答

1

首先,mime_content_type()已弃用,您应该尝试另一种方法来获取MIME值。

我检查了你的代码,它在我的服务器上工作正常,它对我来说工作正常。您应该检查可能阻止下载的INI指令。尝试全新的安装服务器。

另外,在您将问题放入代码段之前,脚本不应该生成输出。