2016-09-30 55 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读取位置中的文件并列出它们,然后重定向到另一个控制器并使用此脚本。 –