Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter - force_download() no output

Fast to explain, but I can't get it to work:

In this simple code, the function force_download simply doesn't make any output.

$this->load->helper('download');
$data = file_get_contents("modulos/".$filename); // Read the file's contents
force_download($filename, $data);
echo $data."/".$filename;

Here I just get a white screen, but the file content is show (well you know, the strange codified content :) I think it is simple enough, I just want the file downloaded with no other effect, am I doing something wrong?

like image 832
Luis Javier Garrido Avatar asked Oct 21 '10 08:10

Luis Javier Garrido


2 Answers

This will work with you

$this->load->helper('download');
$path = file_get_contents(base_url()."modulos/".$filename); // get file name
$name = "sample_file.pdf"; // new name for your file
force_download($name, $path); // start download`
like image 87
Ashraf Talha Avatar answered Oct 21 '22 11:10

Ashraf Talha


Just a note to anyone else who may be having this problem: Make sure you have a file extension on the filename you supply for the first argument to force_download().

CodeIgniter uses this to set the MIME type, and it doesn't seem to work without.

like image 4
Chris Cooper Avatar answered Oct 21 '22 11:10

Chris Cooper