【问题标题】:Unable to download pdf file from the server in codeigniter无法在codeigniter中从服务器下载pdf文件
【发布时间】:2018-01-19 11:54:50
【问题描述】:

我在我的控制器中使用以下代码下载 pdf 文件,这些文件存储在 codeigniter 的应用程序文件夹内的文件夹中。 在视图页面中,我有一个按钮,单击它后,它会重定向到此控制器,然后下载文件。它下载空白页(pdf)。 期待任何建议。

 $filePath = APPPATH.'invoice/'."Let's Sunday#".$this->uri->segment(4).".pdf";
        if(!empty($filePath) && file_exists($filePath)){ 
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=" .$filePath); 
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header("Content-Description: File Transfer");            
        header("Content-Length: " . filesize($filePath));
        flush(); 
        $fp = fopen($filePath, "r");
        while (!feof($fp))
        {
            echo fread($fp, 65536);
            flush(); 
        } 
        fclose($fp); }else{echo "file does not exist";}

【问题讨论】:

  • 你从哪里得到$pdfFilePath
  • 还有,filesize($file) 上的$file 来自哪里?

标签: codeigniter file pdf download


【解决方案1】:

Content-Disposition 中的名称更改为仅文件名而不是完整路径

【讨论】:

    【解决方案2】:

    这是我在 CodeIgniter 应用程序中下载部分的内容:

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

    不同之处在于标头的顺序(可能无关紧要)、包含 expires 和 pragma 标头,以及使用完整基本名称的 Content-Disposition

    该 PDF 文件名的命名约定可能会导致问题...需要测试其他内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-21
      • 2015-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多