I am facing some problems while generating PDF reports from my application in firefox(ubuntu machine).
my code is:
<?php
$path = "path_to_file" ;
$file = "test.pdf";
header('Content-type: application/pdf');
header("Content-disposition: attachment; filename=$file");
readfile($path);
return new Response("Success");
code is working:
when the PDF-report contains data
when size is more than 1 kb
In windows machine
not working:
when report contains no data or size is in bytes.
It is Generating a binary sting in a new tab in browser instead of generating blank-PDF.
Please help me fixing this issue.Thanks in advance.
Got my answer.This may help others.
<?php
$path = "path_to_file" ;
$file = "test.pdf";
header("Content-disposition: attachment; filename= $file"); //Tell the filename to the browser
header("Content-type: application/pdf");//Get and show report format
header("Content-Transfer-Encoding: binary");
header("Accept-Ranges: bytes");
readfile($path); //Read and stream the file
get_curret_user();
I would do it in this way to avoid some problems with browser applications and caching. Give it a try:
<?php
$path = "path_to_file" ;
$file = "test.pdf";
$content = file_get_contents($path);
header("Content-disposition: attachment; filename=\"".$file."\"");
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".strlen($content));
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Expires: 0");
echo $content;
die();
EDIT:
If it's really necessary to use the pdf plugin of the browser instead of downloading and opening it immediately with the associated default pdf reader, please replace
header("Content-type: application/force-download");
with
header("Content-type: application/pdf");
Related
Can someone explain me what I'm doing wrong?
The code below is working only when I'm adding "die" in the end. Without "die" I'm receiving broken gzip file...
$file = $this->connection->result_body;
$fileName = "test.gz";
header("X-Frame-Options: sameorigin");
header("Cache-Control: no-cache");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.$fileName);
header("Content-Type: application/x-gzip");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. strlen($file));
echo $file;die;
i have a code like this :
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-type: application/rar');
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header('Content-Type: application/download');
header('Content-Disposition: attachment; filename='.$fullname);
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($fullname));
$fp = fopen($fullname, "r");
fpassthru($fp);
fclose($fp);
based on above code, download is running, but the tar.gz cannot be opened.
how to solve it? is it possible to download tar.gz?
Content type for GNU zipped archive is application/x-gzip.
You should use below code to set content type
header('Content-type: application/x-gzip');
I have the following code in an attempt to have it so a specific file on the server is downloaded:
$file = 'upload/Order.txt';
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename="basename.$file);
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
All that happens is it causes the browser to download a file, in my case "Order.txt", but its blank, it is just creating this file out of no where. It doesn't do anything with the "upload/" path portion of $file.
Thanks for any help, I find headers really confusing, why I can't just have "download ($file)" is beyond me.
Edit: I'll struggle over a problem for days, finally decide to ask here then immediately after fix it my self.
I changed the location of the file to be in the same as the PHP script, I'd still like to know how to make it work with them being separate however. I also added:
readfile($file);
If I alter with of these 2 changes it doesn't work.
Also slim lined it, new code:
$file = 'Order.txt';
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file);
you should print content of file to stdout after headers:
$file = 'upload/Order.txt';
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
fclose($handle);
echo $contents;
Try change this line:
header("Content-Disposition: attachment; filename='".basename($file)."';");
I'm trying to download a CSV file through the browser. The script is partially working, because so far I managed to display the CSV on screen, but the download is not starting.
Here is what i tried so far:
if(isset($currency)) {
header("Content-Type: application/csv");
header("Content-Disposition: attachment;Filename=Pricelogs.csv");
ob_clean();
$filename = "/tmp/".uniqid().".csv";
exportCSVFile($filename, $currency, $country, $provider);
readfile($filename);
//unlink("'".$filename."'");
} else {
echo "ERR_USERNAME_PASSWORD";
exit();
}
I had already read all questions of this type from FAQ & also tried but it gets open on browser only,instead of downloading.
I had also used header with single quotes.
I also tried header("Content-Type: text/csv");
Also:
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/force-download");
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Transfer-Encoding: binary");
PUT your CSV file URL, it will display in browser in place of force browser to download. You can open it in iframe in your site.
http://docs.google.com/viewer?embedded=true&url=www.yoursite.com/filename.csv
I usually just do:
header('Content-type: text/csv');
header("Content-Disposition: attachment; filename=my_csv_filename.csv");
// print CSV lines here
exit();
I can tell you this combination is working for me:
$bom = chr(0xEF) . chr(0xBB) . chr(0xBF);
header("Content-type: application/csv; charset=UTF-8");
header("Content-Disposition: attachment; filename=$filename.csv");
header("Pragma: no-cache");
header("Expires: 0");
print $bom . $data;
exit;
If I were you, I would first test this plainly (outside of all your buffering), first see that you manage to make this work, and only then test it in your specific set up.
You might try this.
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename.csv\";" );
print $content;
For large files you need to get your output buffer started
add : ob_start(); at the start
ob_clean(); at the end of you file
Here is my php script which executes the download:
$link = 'www.example.com';
$url = urlencode($link);
$image = "http://chart.googleapis.com/chart?chs=75x75&cht=qr&chl=$url&choe=UTF-8&chld=L|0";
$filename = file_get_contents("$image");
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename='qr code';");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
echo $filename;
This downloads a file but it only opens in paint not photoshop. As a hint there doesn't appear to be a file type associated with the file (e.g. png or jpg).
I need all the headers above so this works in all browsers.
Thanks.
Perhaps pass a file extension in your filename header.
header("Content-Disposition: attachment; filename='qr_code.jpg';");