Dan*_*ero 6 php pdf header force-download
可能重复:
如何自动强制下载PDF格式?
我检查了其他论坛,但我不明白他们说什么.我已经检查了这个链接,人们似乎确实得到了解决方案:
http://css-tricks.com/snippets/htaccess/force-files-to-download-not-open-in-browser/
我想做一个用于下载PDF的按钮,该文件太大而无法在线查看.所以我希望该按钮强制浏览器下载文件.询问或不询问用户.将有不同的PDF名称文件,因此我需要使用各种PDF文件的PDF格式下载此按钮.
我已经检查了不同的网站,似乎可以使用htaccess完成,但老实说,我不知道如何做到这一点.有人可以帮我告诉我如何做到这一点!?!?!通过CSS/html有一个简单的方法吗?
对于这么小的细节,我已经被困了很长时间.
我的代码很简单:
<div class="Container for icons">
<a href="hugefile.pdf" alt=""><img href="icon.png" alt=""/>
</div>
Run Code Online (Sandbox Code Playgroud)
有人可以帮我这个吗?
pau*_*sm4 13
您需要修改返回给客户端的HTTP标头才能完成此任务:
这是一个PHP代码示例:
$path = "path/to/file.pdf";
$filename = "file.pdf";
header('Content-Transfer-Encoding: binary'); // For Gecko browsers mainly
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Accept-Ranges: bytes'); // For download resume
header('Content-Length: ' . filesize($path)); // File size
header('Content-Encoding: none');
header('Content-Type: application/pdf'); // Change this mime type if the file is not PDF
header('Content-Disposition: attachment; filename=' . $filename); // Make the browser display the Save As dialog
readfile($path); //this is necessary in order to get it to actually download the file, otherwise it will be 0Kb
Run Code Online (Sandbox Code Playgroud)
而不是AddType你可以尝试ForceType:
<FilesMatch "\.(pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
Run Code Online (Sandbox Code Playgroud)