CodeIgniter Forums

Full Version: Download function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]jadine[/eluser]
Hi,

I would really appreciate if someone can give a few pointers here. I have in my controller a download function which looks like this:

Code:
public function download($file_name){
  
  $mime = 'application/vnd.google-earth.kmz';
  $path = $_SERVER['DOCUMENT_ROOT']."/trackdb/modules/gis/assets/archive/";
  $full_name = $path.$file_name;
  echo($full_name);
  header('Content-Type: '.$mime);
  header("Content-Disposition: attachment; filename=$file_name");
  header("Pragma: no-cache");
  header("Expires: 0");
  readfile("$full_name");
  exit();
  
}

In my view, I have:

Code:
echo anchor('gis/download/'.$result, 'Download KMZ file', 'title="KMZ File"');

$result is the file name passed from the controller.

The generated URL of the link looks like:

http://{Domain}/trackdb/index.php/gis/download/Sites.kmz

Which is the correct file name. The problem is that the file doesn't actually download. It simply downloads a dummy 1 KB .kmz file (the real file in that location is over 2MB).

Many thanks
Jad

El Forum

[eluser]jadine[/eluser]
I've tried using the download helper instead. The message I kept getting is:

Message: file_get_contents(path...) [function.file-get-contents]: failed to open stream: No such file or directory

The problem is that I've tried everything with path, writing it as a relative path, absolute path...etc. and I still keep getting the message.

Code:
public function download($file_name){
  $path = '../assets/archive/';
  $this->load->helper('download');
  $data = file_get_contents($path.$file_name);
  force_download($file_name, $data );
  
}

El Forum

[eluser]jadine[/eluser]
Solved.

Path has to be specified relative to index.php file, not the controller.

El Forum

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

I'm glad you solved your problem.

It's a good idea to always use absolute paths wherever possible. There are some built-in constants to help with that.

Code:
BASEPATH // The path to the ./system directory.
APPPATH // The path to the ./application directory.
FCPATH // The path to the directory containing your index.php file.