Get content of a zip file and echo it to user - php

I wanna make a link for download and all of my file is zip but idont want to my user knows where is my download link
so i wanna get content of zip file and echo it with zip header to user can download it how can i do this?

You should use a "download.php?get=". base64_encode($filename)
When the user click on "download zip", you redirect him with header("Location: download.php?get=". base64_encode($url) to that page.
The headers, here we go
$filename = base64_decode($_GET[get]);
$filepath = "/var/www/domain/httpdocs/download/path/";
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=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
#readfile($filepath.$filename);

Instead of experimenting with headers you could try downloading some 'ready for use' php class. Then your could do something like:
$fileDown = new PHPFileDownload(null, null, 'myFileName', null, 'mypath/myfile.txt');
$fileDown->exportData();

Related

Download file .ZIP with PHP automatically

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

Download A File And Insert Values Into A Database

<?php
// force to download a file
if(isset($_POST['download'])){
$file = "images/dansyo_logo.png";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));
header( "Content-Description: File Transfer");
#readfile($file);
if(#readfile($file)){
echo'proceed';
}else{
echo'failded';
}
}
?>
<form method='post'>
<button name='download'>download</button>
</form>
I have the above code where i want the code to be able to download a file and in the same time insert values into my database.The code is functioning well that is i'm able to download a file.However,nothing is echoed after the file has downloaded or is downloading.
Its not going to echo since header will send the file to browser for download, if you wanted to do any kind of db submission just do it before header sent
$file = "images/dansyo_logo.png";
//Do your DB update here, before the header
if(is_readable($file)) {
// insert into database
} else {
exit(basename($file)." not found.");
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Description: File Transfer");
#readfile($file);
exit();
Note: file_exists() will return true on a directory.

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";
?>

Why does Safari/WebKit appends .html to downloaded files?

I'm having lot of issues with Safari. When I provide a download from a PHP script the downloaded file has ".html" extension appended. Despite the HTTP headers I send to the client, I found them changed in inspector; in particular the mime-type is always set to text/html.
Here what I'm sending to the browser:
<?php
....
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"{$file['name']}\"");
header("Content-Type: ".$file['type']?$file['type']:'application/octet-stream');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($file['file']));
ob_end_flush();
#readfile($file['file']);
Where $file contains:
$file = array(
'name'=>'myfile.zip',
'file'=>'/tmp/myfile.zip',
'type'=>'application/zip'
);
What can I do?

In php Instead of downloading csv file it gets open in the browser

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

Categories