使用Code igniter下载上传的文件

时间:2016-09-30 20:20:15

标签: php codeigniter

我正在尝试在Code igniter中下载上传的文件,但它不起作用。 这是我的控制器(download.php)。如果有人可以发表一个高度欣赏的例子。

<?php  
   class Download extends CI_Controller {   
      public function __construct() { 
         parent::__construct(); 
         $this->load->helper(array('form', 'url')); 
      }     
      public function index() { 
            $this->load->view('vwHomeHeader', $data);
            $this->load->view('vwHomeMenu', $data);
            $this->load->view('download', $data);
            $this->load->view('vwHomeFooter', $data); 
      }     
public function download ($file_path = "") {         
            $this->load->helper('download');            
            $data['download_file'] = $file_path;      
            $this->load->view("download",$data);
            redirect(current_url(), "refresh");                      
        }
    }
?>

这是视图文件(download.php)

<?php
if( ! empty($download_file))
    {
        $data = file_get_contents(base_url("/upload/".$download_file)); 
        $name = $download_file;
        force_download($name, $data);
    }
?>

由于

1 个答案:

答案 0 :(得分:0)

根据需要修改(控制器,除非你想使用锚标签到下载控制器网址,否则不需要视图)

    if($file) {
        $filepath = FCPATH . 'uploads/projects/'.$ProjID.'/'.$file['file_name'];

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . basename($filepath));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($filepath));
        ob_clean();
        flush();
        readfile($filepath);
        exit;
    }