【发布时间】:2015-09-07 05:49:43
【问题描述】:
我正在尝试使用 php 下载 pdf 文件。我可以下载文本文件和图像
我的 PHP 代码:
header("Content-Type: application/octet-stream");
$file = $_GET["file"] .".pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Type: application/pdf"); // pdf might change to another format eg. doc,docx
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
这里是 HTML
部分:
<a href="download.php?file=abc">Download CV</a>
在我的情况下,我可以下载 PDF 文件,它也显示内存大小,但是当我打开它时,会显示如下错误:
加载 PDF 文档失败
我没有收到任何错误,我尝试更改内存限制,但都无法正常工作。任何帮助将不胜感激。
编辑:
我需要下载所有文件格式,并在我的代码中进行一些简单的更改。 示例:
header("Content-Type: application/pdf"); // pdf might change to another format eg. doc,docx
我不想保存浏览器中显示的 PDF 文件,因为在其他格式的情况下,我无法直接从浏览器保存。
当我尝试从下载文件夹打开文件时,它显示错误 喜欢:
Adobe reader 无法打开文件名 abc.pdf,因为它不是受支持的文件类型或文件已损坏
【问题讨论】:
-
如果我可能会问,你为什么需要这个标头 header("Content-Type: application/download");
-
@Saty 不,它不能完全解决我的问题。即使我可以保存,它也会在浏览器上显示内容,因为它是 PDF 文件。但不是任何其他格式