8

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?

yaxe
  • 367
  • 5
  • 26
  • Can you show us the force_download function? If you expect us to help, you have to show us all parts of the problem. Otherwise we have to ask you for it, and that makes us less willing to help. – treeface Oct 21 '10 at 16:57
  • I could Treeface, but it is a Codeigniter System helper, not suposed to be failing, the fuction itself has be long term tested. Anyway, i have solved the problem, it was a misconfiguration of my localhost, who was not undertstanding the headers the function wrotes. – Luis Javier Garrido Oct 22 '10 at 13:20

5 Answers5

5

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`
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Ashraf Talha
  • 51
  • 1
  • 5
4

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.

Chris Cooper
  • 17,276
  • 9
  • 52
  • 70
0

You should not call function after force_download(), Just remove the last line.

Vicky
  • 9,515
  • 16
  • 71
  • 88
0

Remove that echo $data."/".$filename; It should be like this

$this->load->helper('download');
$data = file_get_contents("modulos/".$filename); // Read the file's contents
force_download($filename, $data); 
Asif Mulla
  • 1,652
  • 2
  • 22
  • 32
-1

remove base_url() and do like this

$path= file_get_contents('./uploads/abc.jpg);
Ram Ghadiyaram
  • 28,239
  • 13
  • 95
  • 121