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');
Related
I would like to download a file from a folder on my server and automatically start the download without the person seeing the original file link.
Here is my current script, but it downloads an invalid corrupted file.
$filename = "dsk.zip";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename="./zz/'.$filename.'");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize("./zz/".$filename));
ob_end_flush();
#readfile("./zz/".$filename);
Can you please help me? The file, dsk.zip, is in the folder zz.
$file = "dsk.zip"; // this is what you will get from a param (i.e. ?file=dsk.zip) or from DB query
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename='zz/'.$file");
header("Content-Length: ".filesize($file));
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
readfile('zz/'.$file);
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
am integrating force download using php. when i download the flv files and open in video players its shows error.
$Filename = '/* file name */';
header('Content-Description: File Transfer');
header("Content-type: video/flv");
header("Cache-Control: no-cache");
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename=test.flv' );
header("Pragma: no-cache");
header('Content-length: '.filesize($Filename));
$hFPi = fopen ("$Filename", "rb");
while (!feof($hFPi))
{
$sBuf = fread ($hFPi, filesize($Filename) );
echo $sBuf;
}
fclose ($hFPi);
Please help..
$Filename = '/* file name */';
$path = "../../upload/";
$file = $path.$filename;
savedata1();
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/flv");
header("Content-Transfer-Encoding: binary");
readfile($file);
this will help u. Try it in ur code and u will find ur answer...
This code working great to download mp4, flv, mpeg file from my end.
Here:
$file_name = The Downloaded video file name,
$file_path = Base Path of the original file in the server.
header('Pragma: public'); // required
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Type: video/mpeg");
header("Content-Transfer-Encoding: binary");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0', false);
header('Cache-Control: private', false); // required for certain browsers
ob_clean();
flush();
readfile($file_path);
exit;
Try
$Filename = 'test.flv';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: video/flv");
header("Content-Disposition: attachment; filename=".basename($filename).";" );
header("Content-Transfer-Encoding: binary");
readfile("$Filename ");
This is what I do to force a download of a file (for me it's ZIP but it should work for any file type though). Keep in mind you'll have to tweak it as I store my db information differently than you probably do.
header("Content Description: File Transfer");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must revalidate");
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename(".".$filepath)."");
header("Content-Length: " . filesize(".".$filepath));
header("Content-Transfer-Encoding: binary");
ob_clean();
flush();
readfile(".".$filepath);
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';");