kay*_*oll 48 php automation download
您需要在PHP中添加哪些代码才能在访问链接时自动让浏览器将文件下载到本地计算机?
我特别想到的功能类似于下载站点,提示用户在单击软件名称后将文件保存到磁盘?
Rob*_*her 56
在输出文件之前发送以下标头:
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($File));
header("Connection: close");
Run Code Online (Sandbox Code Playgroud)
@grom:对'application/octet- stream'MIME类型感兴趣.我没有意识到这一点,总是只使用'应用程序/强制下载':)
gro*_*rom 41
以下是发回pdf的示例.
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
header('Content-Transfer-Encoding: binary');
readfile($filename);
Run Code Online (Sandbox Code Playgroud)
@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,没有注册申请/强制下载类型.
vdb*_*der 10
一个干净的例子.
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
95158 次 |
| 最近记录: |