【问题标题】:Codeigniter Force download filesCodeigniter 强制下载文件
【发布时间】:2013-01-27 22:38:50
【问题描述】:

通过阅读 Codeigniter 文档,我正在使用以下代码强制从我的服务器下载文件。

function download($file_id){
        $file = $this->uploadmodel->getById($file_id); //getting all the file details
                                                      //for $file_id (all details are stored in DB)
        $data = file_get_contents($file->full_path); // Read the file's contents
        $name = $file->file_name;;

        force_download($name, $data);
    }

代码是图像的工作文件,但是当它附带 PDF 文件的情况下,它不起作用。我没有针对所有文件扩展名测试它,但由于它不适用于 PDF,它可能不适用于其他各种文件类型。 有什么解决办法吗?

【问题讨论】:

  • 不工作怎么办?不下载?无法在 PDF 阅读器中打开?错误?
  • 是的..当我点击我创建的链接 download/32 时,它只是打开了链接,它是一个空白页面。它是download/$file_id ..
  • .zip、.jpg、.txt、.gif 都可以,但 .pdf 不行
  • 检查您的服务器的错误日志或在 app 文件夹中名为 error_log 的文件中,您可能会在那里找到一些有用的信息。如果两者都没有帮助,请尝试使用其他浏览器或尝试清除当前浏览器的缓存。
  • 当你尝试下载 pdf 文件时,你能打印出你得到 $file 的数组吗..

标签: php codeigniter


【解决方案1】:

我也遇到过类似的问题。我认为问题在于发送到浏览器的某些 mime 和标头。我最终使用了我在这里找到的代码 http://taggedzi.com/articles/display/forcing-downloads-through-codeigniter。使用下面的函数代替 force_download。到目前为止,它对我有用。

    function _push_file($path, $name)
    {
      // make sure it's a file before doing anything!
      if(is_file($path))
      {
        // required for IE
        if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }

        // get the file mime type using the file extension
        $this->load->helper('file');

        $mime = get_mime_by_extension($path);

        // Build the headers to push out the file properly.
        header('Pragma: public');     // required
        header('Expires: 0');         // no cache
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT');
        header('Cache-Control: private',false);
        header('Content-Type: '.$mime);  // Add the mime type from Code igniter.
        header('Content-Disposition: attachment; filename="'.basename($name).'"');  // Add the file name
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: '.filesize($path)); // provide file size
        header('Connection: close');
        readfile($path); // push it out
        exit();
    }
}

希望对你有帮助。

【讨论】:

  • 也许这是旧版本的codeigniter的问题,但现在我使用的是codeigniter 2.2,pdf没有问题,RJ Anoop的回答很有用。
  • @Carlos 嵌入的 URL taggedzi.com/articles/display/… 不再存在,它是一个 404。编辑:我在编辑中删除了它。
【解决方案2】:

它也适用于.pdfs。 检查文件的路径 - 这可能是问题所在。
我也有这个问题,但是当我更正文件的路径时,它工作得很好。
以下是我编写代码的方式:

if($src ==  "xyyx")
{
$pth    =   file_get_contents(base_url()."path/to/the/file.pdf");
$nme    =   "sample_file.pdf";
force_download($nme, $pth);     
}

【讨论】:

  • 最初没有工作。只是显示一个空白页。我忘记添加“.pdf”扩展名
  • 我得到failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
  • 这不是脚本的问题,而是您请求的资源的问题。 Web 服务器正在返回“禁止”状态代码。
【解决方案3】:

试试这个,我认为它是解决该问题的最佳实践。

function download($file_id)
{
        $this->load->helper('file'); // Load file helper
        $file = $this->uploadmodel->getById($file_id); //Get file by id
        $data = read_file($file->full_path); // Use file helper to read the file's
        $name = $file->file_name;;

        force_download($name, $data);
}

【讨论】:

    猜你喜欢
    • 2011-08-28
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 2013-06-20
    相关资源
    最近更新 更多