【问题标题】:php pdf download - can't open the downloaded file correctlyphp pdf下载 - 无法正确打开下载的文件
【发布时间】:2013-12-07 17:10:20
【问题描述】:

在我的网页上可以下载 pdf 文件 (285kB)。问题是您将网站下载为 pdf 而不是我要下载的 pdf 文件。

所以我不能用 Adob​​e Reader 打开下载的 pdf,但必须用记事本打开文件,内容是

<!DOCTYPE html><html lang="en"><he...

这是下载文件的代码:

 $filepath = "/download/";
    $filename = "filename";
    header('Content-disposition: attachment; filename='.$filename);
    header('Content-type: application/octet-stream' );
    header('Content-Length: '. filesize($filepath.$filename));
    readfile($filepath.$filename);
    exit;

你能帮帮我吗?

【问题讨论】:

    标签: php pdf download


    【解决方案1】:

    您使用了错误的 Content-Type 标头。使用

    header('Content-type: application/pdf' );
    

    定义在RFC 3778

    【讨论】:

    • 您确定$filename 包含指向有效pdf 文件的路径吗?
    • 是的,我是。我已经尝试过没有路径并将pdf放在主目录中...->同样的问题
    • 显示下载链接的html
    • 我可以请你跟随我和 Jospeh118 的 cmets 吗?写了一些更多的信息:)
    【解决方案2】:

    抱歉,由于我的声誉低,我无法发表评论。这就是为什么发布作为答案。你可以试试

    header('Content-type: application/pdf' );
    header('Content-disposition: attachment; filename='.$filename.'.pdf');
    header('Content-disposition: attachment; filename='.$filename.'.pdf');
    

    【讨论】:

      【解决方案3】:

      您是否添加了带有文件名的扩展名?像这样……

      $filename = "filename.pdf";
      $filepath = "/download/".$filename;
      
      if (file_exists($filePath) {
      header('Content-Disposition: attachment; filename='.basename($filepath));
      header('Content-type: application/zip' );
      header('Content-Length: '. filesize($filePath));
      readfile($filepath);
      exit;
      }
      

      【讨论】:

      • 你怎么称呼这个脚本?
      • 这个小脚本位于您可以下载文件的同一页面上。单击“下载”-提交按钮后,将再次打开相同的页面。开头只有一个 if 语句: if(isset($_POST['download']))
      • 你是如何发送数据的?提供给我们
      • 还为我们提供了您的目录层次结构..您的路径可能是错误的
      • 对不起,我不明白你的意思。
      猜你喜欢
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多