2016-09-30 75 views
0

我試圖在代碼點火器中下載上傳的文件,但它不起作用。 這是我的控制器(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); 
    } 
?> 

感謝

+0

請描述預期的行爲和實際的 – YakovL

回答

0

,除非你想用錨修改,您認爲合適(控制器,不需要一個視圖標記到下載控制器url)

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; 
    } 
+0

嗨James,t hank回覆...但我真正需要的是顯示「上傳」文件夾中的現有文件,當用戶選擇文件,然後下載 – userit75

+0

然後使用PHP讀取位置中的文件並列出它們,然後重定向到另一個控制器並使用此腳本。 –