Download file .ZIP with PHP automatically - php

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);

Related

PDF file not shownig on mozila but work fine in chrome .

I am using this code to download pdf file . its work fine in chrome but in mozila it create .htm file .Any can help me about 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/pdf");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Transfer-Encoding: binary");
readfile('phpToPDF/examples/pdf/'.$filename);
you need to terminate current script. that's why you are getting .htm extension on Mozilla.
use exit(); at the end of your code.
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/pdf");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Transfer-Encoding: binary");
readfile('phpToPDF/examples/pdf/'.$filename);
exit();

Downloading A File Located Above public_html

I have a document hosted in a folder above public_html for security reasons - moving the files into the web root is not an option.
I'm trying to access them via a readfile() but whatever I download winds up being 0 bytes and an empty file. I think the problem is that the "Content-Length" header isn't getting a correct value because filesize($target) is giving no output.
Any thoughts? Code included:
header('Content-Description: File Transfer');
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Transfer-Encoding: binary");
header("Pragma: public");
$file="../docs/$c/$path";
$size=filesize($file); //i think this line is the problem
header ("Content-Length: $size");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename\"");
readfile($file_url);
I don't see $file_url defined in your code. Maybe it should be $file:
header('Content-Description: File Transfer');
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Transfer-Encoding: binary");
header("Pragma: public");
$file="../docs/$c/$path";
$size=filesize($file); //i think this line is the problem
header ("Content-Length: $size");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename\"");
readfile($file);
I also don't see $filename defined.

Header file download creates new file instead of sending one from server (PHP)

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)."';");

PHP Unable to write the content in PDF

After executing the below code, Browser automatically Downloading PDF Document. When i open that PDF, i am getting below Error.
I got below snippet idea from source
<?php
$filename = "yourfilename.pdf";
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/pdf");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
echo "my contents should display in PDF Document";
?>

Force download FLV file not working in PHP

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);

Categories