3 回答
![?](http://img1.sycdn.imooc.com/5458464a00013eb602200220-100-100.jpg)
TA贡献2051条经验 获得超10个赞
在输出文件之前发送以下标头:
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($File));
header("Connection: close");
@grom:对'application / octet- stream'MIME类型感兴趣。我没有意识到这一点,总是只使用'应用程序/强制下载':)
![?](http://img1.sycdn.imooc.com/533e4d470001a00a02000200-100-100.jpg)
TA贡献1815条经验 获得超10个赞
以下是发回pdf的示例。
header('Content-type: application/pdf');header('Content-Disposition: attachment; filename="' . basename($filename) . '"');header('Content-Transfer-Encoding: binary');readfile($filename);
@Swish我没有找到应用程序/强制下载内容类型来做任何不同的事情(在IE和Firefox中测试)。是否有理由不发回实际的MIME类型?
另外在PHP手册中,Hayley Watson发布了:
如果您希望强制下载和保存文件而不是呈现文件,请记住没有像“application / force-download”这样的MIME类型。在这种情况下使用的正确类型是“application / octet-stream”,并且使用其他任何东西仅仅依赖于客户端应该忽略无法识别的MIME类型并使用“application / octet-stream”的事实(参考:章节) RFC 2046的4.1.4和4.5.1)。
此外,根据IANA,没有注册申请/强制下载类型。
![?](http://img1.sycdn.imooc.com/5458657e000125a302200220-100-100.jpg)
TA贡献1831条经验 获得超9个赞
一个干净的例子。
<?php header('Content-Type: application/download'); header('Content-Disposition: attachment; filename="example.txt"'); header("Content-Length: " . filesize("example.txt")); $fp = fopen("example.txt", "r"); fpassthru($fp); fclose($fp);?>
- 3 回答
- 0 关注
- 647 浏览
添加回答
举报