Codeigniter强制下载文件 [英] Codeigniter Force download files

查看:372
本文介绍了Codeigniter强制下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 功能下载(

$ file_id){
$ file = $ this-> uploadmodel-> getById($ file_id); //获取所有文件详细信息
// for $ file_id(所有详细信息存储在DB中)
$ data = file_get_contents($ file-> full_path); //读取文件的内容
$ name = $ file-> file_name ;;

force_download($ name,$ data);
}

代码是图片的工作文件, PDF文件,它不工作。我没有测试它的所有文件扩展名,但由于它不工作的PDF,它可能不适用于其他各种文件类型。
任何解决方案?

解决方案

我有类似的问题,我认为问题存在于某些mime和headers发送到浏览器。我最终使用我在这里找到的代码 http://taggedzi.com/articles/display/forcing -downloads-through-codeigniter 。使用下面的函数而不是force_download。它为我工作到目前为止。

  function _push_file($ path,$ name)
{
//确保它是一个文件做任何事情!
if(is_file($ path))
{
//需要IE
if(ini_get('zlib.output_compression')){ini_set('zlib.output_compression' 'Off'); }

//使用文件扩展名获取文件mime类型
$ this-> load-> helper('file');

$ mime = get_mime_by_extension($ path);

//构建头以正确推出文件。
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))。
header('Cache-Control:private',false);
header('Content-Type:'。$ mime); //从Code igniter添加mime类型。
header('Content-Disposition:attachment; filename ='。basename($ name)。''); //添加文件名
header('Content-Transfer-Encoding:binary');
header('Content-Length:'.filesize($ path)); //提供文件大小
header('Connection:close');
readfile($ path); // push it out
exit();
}

希望它有帮助。


By going through the codeigniter documentation, I am using following code to force download files from my server.

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);
    }

The code is working file for images, but when it comes with the case of PDF files, it is not working. I have not tested it for all file extensions, but since it is not working for PDF, it might not work for other various file types. Any solution?

解决方案

I've had similar problems, I think the problem resides in certain mime's and headers sent to browsers. I've end up using the code I found here http://taggedzi.com/articles/display/forcing-downloads-through-codeigniter. Use the function below instead of force_download. It has worked for me so far.

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();
}

Hope it helps.

这篇关于Codeigniter强制下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆