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();
Related
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
I've got a web from that has two buttons for submitting, one sends an email with pdf attached, this works perfectly.
The second button is to download the pdf, this is the problem. I am saving the pdf in a temp file before download but after it is downloaded the file doesn't open and it is corrupt. The pdf is about 30KB. I have tried solutions to similar questions but always the same result, the pdf won't open.
This didn't work
$fileName = "file.pdf";
$file_name = ("temp/file.pdf");
file_put_contents($file_name, $pdf_content);
$filepath=$file_name; //file location
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);
This didn't work
$path = $_SERVER['DOCUMENT_ROOT']."/temp/"; // change the path to fit your websites document structure
$fullPath = $path.$fileName;
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment;
filename='.basename($fullPath));
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($fullPath));
readfile($fullPath);
exit;
This didn't work
set_time_limit(0); // disable timeout
$file = $root_path.'/full-tile-book.pdf';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="NewName.pdf"');
header('Content-Length: ' . filesize($file));
$f = fopen($file, 'rb');
fpassthru($f);
fclose($f);
exit;
This didn't work
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=file.pdf');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($filepath)); // provide file size
header('Connection: close');
readfile($filepath);
exit();
The file is always in the temp folder on the server and that works fine so somewhere in the download the file is getting corrupted.
I don't care how the download is done, pdf or oclet-stream or any other way.
I removed the : Content-Type header and it works for me.
Regards
I'm really struggling to get my application to open a pdf when the user clicks on a link.
So far the anchor tag redirects to a page which sends headers that are:
$filename='./pdf/jobs/pdffile.pdf;
$url_download = BASE_URL . RELATIVE_PATH . $filename;
header("Content-type:application/pdf");
header("Content-Disposition:inline;filename='$filename");
readfile("downloaded.pdf");
this doesn't seem to work, has anybody successfully sorted this problem in the past?
Example 2 on w3schools shows what you are trying to achieve.
<?php
header("Content-type:application/pdf");
// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename=\"downloaded.pdf\"");
// The PDF source is in original.pdf
readfile("original.pdf");
?>
Also remember that,
It is important to notice that header() must be called before any
actual output is sent (In PHP 4 and later, you can use output
buffering to solve this problem)
$name = 'file.pdf';
//file_get_contents is standard function
$content = file_get_contents($name);
header('Content-Type: application/pdf');
header('Content-Length: '.strlen( $content ));
header('Content-disposition: inline; filename="' . $name . '"');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
echo $content;
There are some things to be considered in your code.
First, write those headers correctly. You will never see any server sending Content-type:application/pdf, the header is Content-Type: application/pdf, spaced, with capitalized first letters etc.
The file name in Content-Disposition is the file name only, not the full path to it, and altrough I don't know if its mandatory or not, this name comes wrapped in " not '. Also, your last ' is missing.
Content-Disposition: inline implies the file should be displayed, not downloaded. Use attachment instead.
In addition, make the file extension in upper case to make it compatible with some mobile devices. (Update: Pretty sure only Blackberries had this problem, but the world moved on from those so this may be no longer a concern)
All that being said, your code should look more like this:
<?php
$filename = './pdf/jobs/pdffile.pdf';
$fileinfo = pathinfo($filename);
$sendname = $fileinfo['filename'] . '.' . strtoupper($fileinfo['extension']);
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename=\"$sendname\"");
header('Content-Length: ' . filesize($filename));
readfile($filename);
Technically Content-Length is optional but it is important if you want the user to be able to keep track of the download progress, and detect if the download was interrupted before the end. When using it you have to make sure you won't be send anything along with the file data. Make sure there is absolutely nothing before <?php or after ?>, not even an empty line.
I had the same problem recently and this helped me:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="FILENAME"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize("PATH/TO/FILE"));
ob_clean();
flush();
readfile(PATH/TO/FILE);
exit();
I found this answer here
Can you try this, readfile need the full file path.
$filename='/pdf/jobs/pdffile.pdf';
$url_download = BASE_URL . RELATIVE_PATH . $filename;
//header("Content-type:application/pdf");
header("Content-type: application/octet-stream");
header("Content-Disposition:inline;filename='".basename($filename)."'");
header('Content-Length: ' . filesize($filename));
header("Cache-control: private"); //use this to open files directly
readfile($filename);
You need to define the size of file...
header('Content-Length: ' . filesize($file));
And this line is wrong:
header("Content-Disposition:inline;filename='$filename");
You messed up quotas.
header("Content-type:application/pdf");
// It will be called downloaded.pdf thats mean define file name would be show
header("Content-Disposition:attachment;filename= $fileName ");
// The PDF source is in original.pdf
readfile($file_url);
Hello I am trying to generate a php powered download link for my program, I had copied and pasted some old code I had laying around which worked in the past, and it still seems to work okay except for 2 issues.
Issue 1: No matter what it always download the file as link.php not the filename
Issue 2:The filesize isn't sent to the browser(not really that concerned about this)
Mainly I need to know what I am doing wrong in setting the filename, here is my code below:
$file = $_GET['file'].'.exe';
if ($_GET['DL'] == "GO") {
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browsers
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'. basename('MIRROR/'.$file) . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize('MIRROR/'.$file));
ob_clean();
flush();
readfile('MIRROR/'.$file);
die();
}
Try taking out the trailing ;:
header('Content-Disposition: [..snip...] . basename($file) . '";');
^--- here
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.