Php 如何在Internet Explorer中下载文件作为文档?

Php 如何在Internet Explorer中下载文件作为文档?,php,internet-explorer,download,Php,Internet Explorer,Download,我的文件下载为文档在IE中不起作用。如何用PHP编写代码,以便在Internet Explorer中正确下载文件设置正确的HTTP头 <?php header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename="downloaded.pdf"'); readfile('original.pdf'); ?> 检查以确保标头声明没有错误。(使用PHP时,您必须确保内容类

我的文件下载为文档在IE中不起作用。如何用PHP编写代码,以便在Internet Explorer中正确下载文件

设置正确的HTTP头

<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
?>

检查以确保标头声明没有错误。(使用PHP时,您必须确保内容类型声明正确,并且是PHP文件的第一行。)

以下是我在过去三年中一直使用的代码,它运行良好。 刚才我在所有浏览器IE6、IE7、IE8中进行了测试,所有这些浏览器都运行良好。 $fn是文件的完整路径。 确保没有其他额外的空格或换行符,在将文件刷新到浏览器之前,我已经使用了ob_clean

如果您没有在上面发送其他html代码,则更好。。。因此,请单独使用,不要包含其他内容。否则用ob_start()启动代码;所以在ob_clean()的生产线上不会有任何额外的字符

$filename = "$fn";
ob_clean();
header("Cache-Control: no-store");
header("Expires: 0");
header("Content-Type: application/octet-stream");
header("Content-disposition: attachment; filename=\"".basename($filename)."\"");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));
readfile($filename);
我有一个文件管理模块,我上传文件,用户可以下载。 如果你仍然有问题或无法解决,只需再添加几行你的问题,任何人都会对此作出回应

我之所以使用更多的标题是为了确保它能正常工作,因为最初我在IE 6上遇到了问题

对于安全服务器,HTTPS将前两个标头替换为此标头

header("Cache-Control:  maxage=1");
header("Pragma: public");

这样对我来说很有效。

那是。。。不幸的是?Python处理XML在Linux中不起作用。在缓存控制中,您可以像这样使用“缓存控制:没有存储,没有缓存,必须重新验证”我为安全服务器添加了两行代码。非常感谢ob_clean部分,当您不在页面开始处处理标题时,这确实是一个非常好的攻击=)