php Codeigniter 强制下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14835671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:02:37  来源:igfitidea点击:

Codeigniter Force download files

phpcodeigniter

提问by prakashchhetri

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

通过阅读 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);
    }

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?

该代码是用于图像的工作文件,但是当涉及 PDF 文件时,它不起作用。我尚未针对所有文件扩展名对其进行测试,但由于它不适用于 PDF,因此它可能不适用于其他各种文件类型。有什么解决办法吗?

回答by Carlos Quijano

I've had similar problems. I think the problem resides in certain mime's and headers sent to the browser(s). 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.

我也遇到过类似的问题。我认为问题在于发送到浏览器的某些 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();
    }
}

Hope it helps.

希望能帮助到你。

回答by RJ Anoop

It is working for .pdfs also. Check the path to the file - that might be the problem.
I, too, had that problem, but when I corrected the path to the file, it worked perfectly.
The following is how I wrote the code:

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

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