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


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

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

In my view, I have:

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:


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

El Forum

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.

public function download($file_name){
  $path = '../assets/archive/';
  $data = file_get_contents($path.$file_name);
  force_download($file_name, $data );

El Forum


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

El Forum

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.

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.