【问题标题】:cannot download pdf in PHP无法在 PHP 中下载 pdf
【发布时间】:2020-07-24 10:43:39
【问题描述】:

我想强制浏览器使用 php.ini 中的readfile 从服务器下载 PDF 文件。 这是我的代码:

$file = '/DIR/file.pdf';
            if (file_exists($file)) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename="'.basename($file).'"');
                header('Expires: 0');
                header('Cache-Control: must-revalidate');
                header('Pragma: public');
                header('Content-Length: ' . filesize($file));
                readfile($file);
                exit;
            }

在该浏览器显示一些未知字符而不是下载 PDF 文件之后。我尝试了不同的浏览器,但都没有工作。 如何强制浏览器下载文件?
输出是这样的:

请注意,如果我在浏览器中输入文件目录,文件会正确下载。

【问题讨论】:

  • 我觉得内容头不对,你试过这个:header("Content-type:application/pdf");?
  • 使用Content-Type: application/pdf
  • @KIKOSoftware 我试过了,但没用
  • @MarkusZeller 我试过了,但没用
  • header('Content-Encoding: identity')header("Content-type:application/pdf")

标签: php pdf download readfile


【解决方案1】:

只需将内容类型设置为 application/pdf 并将编码设置为身份

 $file = '/DIR/file.pdf';
        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('HTTP/1.1 200 Ok');
            header('Content-Encoding: identity');
            header("Content-type: application/pdf");

            header('Content-Disposition: attachment; filename="'.basename($file).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
          //  header('Pragma: public'); //not needed (unless you're using HTTP 1.0)
            header('Content-Length: ' . filesize($file));
            readfile($file);
            exit;
        }else{
           header('HTTP/1.1 404 Not found'); //throws 404 not found if the server can't find / access the file
           echo ("file $file not found");
           exit;

}

在我的本地系统上它可以工作(它打开 adobe-pdf)

最后的想法:检查文件路径,你的路径是绝对的(从文件系统根目录搜索)你可能希望它相对于你的网络服务器的 wwwroot:./DIR/file.pdf

【讨论】:

  • 感谢您的回复,但仍然无法正常工作。我应该更改服务器中的某些设置吗?
  • 无论如何检查您的服务器是否可以将 application/pdf 作为 mime 类型处理
  • 可能是因为页面被缓存了。清空缓存并重试。
  • @DDS 我应该如何检查 pdf 是 mime 类型?
  • @KIKOSoftware 我不这么认为。我按CTRL+F5清除缓存,然后重新进入
【解决方案2】:

你可以使用它。然后尝试直接访问这个PHP页面的url。

<?php
    $file_url = "https://path/to/your/pdf";


    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
    readfile($file_url);

?>

然后将其粘贴到您的 .htaccess 中

<ifmodule mod_headers.c="">
    SetEnvIf Origin "^(.*\.domain\.com)$" ORIGIN_SUB_DOMAIN=$1
    Header set Access-Control-Allow-Origin "%{ORIGIN_SUB_DOMAIN}e" env=ORIGIN_SUB_DOMAIN
    Header set Access-Control-Allow-Methods: "*"
    Header set Access-Control-Allow-Headers: "Origin, X-Requested-With, Content- 
    Type, Accept, Authorization"
</ifmodule>

【讨论】:

  • 感谢您的大力帮助。该代码有效,浏览器强制下载文件。但是还有另一个问题。下载的文件已损坏,任何 PDF 阅读器都无法打开。
  • 请注意服务器上的文件是可以的。它可以正确打开和下载。
  • 你能用另一个PDF文件试试看结果是否一样?
  • 我将文件从 PC 上传到服务器,然后使用新 PDF 文件的 URL。结果是一样的。下载的 PDF 已损坏,无法打开。
  • 您的下载链接使用的是 http 还是 https?应该是 https
猜你喜欢
  • 1970-01-01
  • 2015-10-06
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-30
相关资源
最近更新 更多