PHP Force下载不起作用

时间:2012-08-07 11:28:41

标签: php header download content-type content-disposition

这是我的问题。我试图使用标题下载文件。这是我的代码:

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

然而,唯一的结果是页面显示文件的内容(它是文本文件)或一串奇怪的符号,如果文件是image / zip / exe等

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

1 个答案:

答案 0 :(得分:1)

首先,不推荐mime_content_type(),您应该尝试其他方法来获取MIME值。

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

此外,在您提交问题的代码段之前,脚本不应生成任何输出。