Codeigniter Force Download Files

Codeigniter Force download files

I've had similar problems. I think the problem resides in certain mime's and headers sent to the browser(s). I've end up using the code I found here http://taggedzi.com/articles/display/forcing-downloads-through-codeigniter. Use the function below instead of force_download. It has worked for me so far.

    function _push_file($path, $name)
{
// make sure it's a file before doing anything!
if(is_file($path))
{
// required for IE
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }

// get the file mime type using the file extension
$this->load->helper('file');

$mime = get_mime_by_extension($path);

// Build the headers to push out the file properly.
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime); // Add the mime type from Code igniter.
header('Content-Disposition: attachment; filename="'.basename($name).'"'); // Add the file name
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($path)); // provide file size
header('Connection: close');
readfile($path); // push it out
exit();
}
}

Hope it helps.

Force download in CodeIgniter

You are passing null to the filename parameter, you need to pass it to the data parameter (2nd parameter), so your code should be like this

public function download($id,$file_name)
{
$this->load->helper('download');
force_download(FCPATH.'/uploads/files/'.$file_name, null);
}

also, don't concatenate the base_url() with the path, you can pass the path to the base_url('/path/to/file') function and it'll return the link to that file.

I notice that you are passing the $id variable but it's not used, are you using it in another part of the code that is not included?

And why do you even have a function to just call the force_download() function? can't you just call it inside the Controller?

How to force download zip file in codeigniter

Use below function to download the file

function auto_download($path, $name) {
if(is_file($path)) {
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }

$this->load->helper('file');

$mime = get_mime_by_extension($path);

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($path));
header('Connection: close');
readfile($path);
exit();
}

}

PHP force_download is downloading file with filename specified but calling it 'file.pdf'

It turns out that another programmer put $pdfobj->ezStream( array('compress' => 0 ) ); where the PDF was being generated and that was the cause of the problem. I removed it and everything works perfectly now.

Not force_download with images and pdf files in codeigniter

As I already wrote, make check in if elseif else or even maybe better switch case block with checking file extension before download/preview code. Something like:

public function download($file)
{
//get the file extension
$info = new SplFileInfo($file);
//var_dump($info->getExtension());

switch ($info->getExtension()) {
case 'pdf':
case 'png':
case 'jpg':
$contentDisposition = 'inline';
break;
default:
$contentDisposition = 'attachment';
}

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
// change inline to attachment if you want to download it instead
header('Content-Disposition: '.$contentDisposition.'; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
}
else echo "Not a file";
}


Related Topics



Leave a reply



Submit