I am trying to create a simple digital store web application using codeigniter.
I would like to use the force download helper function so that the real url of the file will not be known by the user.
I tried following the documentation in the codeignter - it works but the file get corrupted.
Here is my code
//* Get the website name
$site = $this->Settings_model->getApllicationInfo();
$sitename = $site->set_site_name;
//* Prepare file for downloading
$filename = $sitename.'-'.$item_info->item_id.'-'.$item_info->item_name;
$locate = './static/files/zips/'.$file;
force_download($locate, $filename);
It downloads the file but it get broken please help me or give me any other suggestion I can use.
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();
}
}
I hope you are using file extension on the filename you supply for the first argument to force_download(). Because Codeigniter uses MIME-type. I hope it will work for you
Related
I want to export data in xls format using PHP. But instead of download information, they are displayed in my web page.
//connection to database
//building of all information -> ($righe)
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: esporta_utenti.xls");
print $righe;
exit();
$righe is my tuple
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 ($file_name)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime); // file mime type
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file_name)); // provide file size
header('Connection: close');
readfile($file_name); // push it out
exit();
This code was originally Published here http://davidwalsh.name/php-force-download . And can be referred for a detailed explanation of the Code.
I'm trying to create a download link for a PDF file. It works fine if Adobe Reader is installed, but if I uninstall it, it tries to open in browser and fails.
Please tell me what is the actual problem about this?
Thanks in Advance.
The problem is that when adobe reader is installed than it automaitcally gets the header of .pdf file and show the file.
You can use this.It will always prompt to download the file ..
$path="uploads/";
$actualfilename=$path.$row["name"];
//name of the file or location of file..
if($typeofview=="download") {
#readfile($actualfilename);
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . $actualfilename. '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($actualfilename));
header('Accept-Ranges: bytes');
exit;
}
This is a duplicate of this question but here is the answer:
$path_to_file = '/var/www/somefile.pdf'; //Path to file you want downloaded
$file_name = "somefile.pdf"; //Name of file for download
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header('Content-Transfer-Encoding: binary');
readfile($path_to_file);
die();
I have a problem with forcing download of some mp3/zip files.
The problem is that some users experiences that the whole file isn't downloaded, and i can't figure out why..
if(is_file($file_name))
{
/*
Do any processing you'd like here:
1. Increment a counter
2. Do something with the DB
3. Check user permissions
4. Anything you want!
*/
// required for IE
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
// get the file mime type using the file extension
switch(strtolower(substr(strrchr($file_name,'.'),1)))
{
case 'mp3': $mime = 'audio/mpeg'; break;
case 'zip': $mime = 'application/zip'; break;
default: $mime = 'application/force-download';
}
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 ($file_name)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file_name)); // provide file size
header('Connection: close');
readfile($file_name); // push it out
exit();
}
here is my code..
Any ideas?
Thanks for all of your help,
but i found another solution to my problem, and used mod_xsendfile instead. It has done the job for me :)
I'm trying to make a file that send a pdf file to the visitor.
I have file: download.php
and this is his content:
header('Content-disposition: attachment; filename='.$_GET['file']);
header('Content-type: application/pdf');
readfile($_GET['file']);
for some reason, the file sending an empty pdf file 183 bytes.
any advice?
thanks.
You should provide the full path to the file for readfile() not just a filename.
First off, you really should first check if the file exists with file_exists
Second ... this seems pretty insecure since you allow specifying the filename through a global $_GET parameter. What if I would try to download your config file like download.php?file=../application/settings/config.ini? You should first filter the $_GET parameter and make sure the file specified is allowed for being downloaded.
Try this:
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/pdf');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($filepath)) . ' GMT');
header('Content-disposition: attachment; filename=' . $pathinfo['filename'] . '.pdf');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($filepath)); // provide file size
header('Connection: close');
readfile($filepath);
exit();
I have the following code
header("Content-Description: File Transfer");
header('Content-Type: audio/mp3');
header("Content-Disposition: attachment; filename=" . $arrResults['audioLink'] . ".mp3");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Transfer-Encoding: binary");
$strLink = 'http://ag-org.s3.amazonaws.com/members/tele_classes/' . $arrResults['audioLink'];
ob_clean();
flush();
readfile($strLink);
However when the download link is clicked that executes this code it always returns a 0 byte file. All files are set to public in the bucket.
What am I doing wrong here?
Is the .mp3 extension really not part of the filename as it's stored on S3?
You're appending .mp3 to the filename in the Content-Disposition header, but not the URL you're passing to readfile.