I'm trying to download uploaded files in Code igniter, but it doesn't work. Here is my controller(download.php). If someone can post an example highly appreciate.
<?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");
}
}
?>
And here is the view file(download.php)
<?php
if( ! empty($download_file))
{
$data = file_get_contents(base_url("/upload/".$download_file));
$name = $download_file;
force_download($name, $data);
}
?>
Thanks
modify as you see fit (controller, no need for a view unless you want to use an anchor tag to the download controller 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;
}