Codeigniter - downloading file with use of force_download function Codeigniter - downloading file with use of force_download function codeigniter codeigniter

Codeigniter - downloading file with use of force_download function


This works for me.

ob_clean(); $data = file_get_contents("localhost/qlip/uploads/filename.jpg"); //assuming my file is on localhost$name = 'document.jpg'; force_download($name,$data); 


try this code follow:

<?phpfunction downloadFile($file){   $file_name = $file;   $mime = 'application/force-download';   header('Pragma: public');       header('Expires: 0');           header('Cache-Control: must-revalidate, post-check=0, pre-check=0');   header('Cache-Control: private',false);   header('Content-Type: '.$mime);   header('Content-Disposition: attachment; filename="'.basename($file_name).'"');   header('Content-Transfer-Encoding: binary');   header('Connection: close');   readfile($file_name);       exit();}?>

and call function :

<?phpdownloadFile("./files/image.jpg");?>

provided that the file "image.jpg" is the correct address


If you prefer, you could try this:

function download() {    // asumming you have http://www.domain.com/index.php/controller/download/file_name/extension/    $extension = $this->uri->segment(4); // file extension    $file_name = $this->uri->segment(3) . '.' . $extension; // file name    $file_path = FCPATH . 'application/documentos/' . $file_name; // absolute path to file    if (is_file($file_path)) {        $mime = 'application/force-download';        header('Pragma: public');            header('Expires: 0');                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');        header('Cache-Control: private',false);        header('Content-Type: ' . $mime);        header('Content-Disposition: attachment; filename="' . $file_name . '"'); file name        header('Content-Transfer-Encoding: binary');        header('Connection: close');        readfile(base_url() . 'application/documentos/' . $file_name); // relative path to file           exit();    } else {        redirect('/welcome/');    }}