For reference, I have already read and tried the answers in these and several other threads:
Creating and serving zipped files with php
Opening downloaded zip file creates cpgz file?
I have a zip file on my server.
When I use Filezilla to move that Zip file from my server to my Mac, I can open it normally.
When I use this PHP code to download the Zip file to my Linux machine, it opens normally.
When I use this PHP code to download the Zip file to my Mac, using Safari or Firefox, I get an error saying "Decompression Failed" or "The structure of the archive is damaged" or I get a .cpgz file - which I believe means that the computer is zipping the file, not unzipping it.
Here is the PHP code I am using to deliver the zip file.
$zipname = "myfile.zip";
$zippath = "/path/to/" . $zipname;
if ($downloadzip = fopen ($zippath, "r")) {
$fsize = filesize($zippath);
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$zipname."\"");
header("Content-length: $fsize");
header('Content-Transfer-Encoding: binary');
#header("Cache-control: private"); //use this to open files directly
echo fpassthru($downloadzip); // deliver the zip file
}
fclose ($downloadzip);
I found some headers that work. I don't really know or care why it work, I am just happy it works... I tried a ton of different things, .htaccess files, php.ini / zlib settings.
Here's the answer
http://perishablepress.com/http-headers-file-downloads/
$zipName = 'myfile.zip';
$zipPath = 'mydirectory/' . $zipName;
if (file_exists($zipPath)) {
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=\"".$zipName."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($zipPath));
ob_end_flush();
#readfile($zipPath);
}
Often the issue is caused by extra characters that have been printed or echo'd to the page before you read out the file. Even a space will cause the failure. To fix that issue, call ob_end_clean(); before you read the file which will clear the output buffer and turn off buffering.
But keep in mind you can have nested output buffers, and this will corrupt your download as well (cheers to Vladamir for figuring this out). So to clear the output buffer completely run this before you read your file:
while (ob_get_level()) {
ob_end_clean();
}
This will clear out your entire buffer and you won't have any extra characters to mess up your download.
For those interested i've pasted my download script below. My zip files now download perfectly, and so far this works great.
if (file_exists($zip_file_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");
//We can likely use the 'application/zip' type, but the octet-stream 'catch all' works just fine.
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename='$zip_file_name'");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($zip_file_path));
while (ob_get_level()) {
ob_end_clean();
}
#readfile($zip_file_path);
exit;
}
Here is what works
$zipName = 'myfile.zip';
$zipPath = 'mydirectory/' . $zipName;
if (file_exists($zipPath)) {
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=\"".$zipName."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($zipPath));
ob_end_flush();
#readfile($zipPath);
}
Well, I presume you know that your $fsize variable is not being written to that header because it's enclosed by quotes.
You could try something like this:
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename=\"".$zipname."\"');
header('Content-Type: application/zip');
Related
I have a PHP script called retrieve.php that compresses uploaded files from a directory in to a .zip file and then outputs them to the stream, so said file can be downloaded.
The script works in all browsers except Firefox.
After searching online for quite a while now and coming across this question: PHP download script returns download-file.php instead of filename and still having the same issue, I'm stumped and have no clue what's going wrong.
This is the code being used to download the file:
if ($zippedElements >= 1) {
// die($zipFilename);
$zipFilename = "download-".time().".zip";
// Send zip to recipient
header("Content-Dispositon: attachment; filename=\"$zipFilename\"");
header("Content-Length: ".filesize($zipFileName));
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
header("Connection: Keep-Alive");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
header("Accept-Ranges: bytes");
readfile($zipFileName);
} else return;
I've tried setting the header to different values, such as content-type: application/force-download, adding the asterisk (*) after filename, etc., but all to no avail.
The result in Firefox looks as follows:
Any help with the matter would be much appreciated.
You have inconsistencies in the variable name $zipFilename - the N is uppercased sometimes.
$zipFilename = __FILE__;
// Send zip to recipient
header("Content-Dispositon: attachment; filename=\"$zipFilename\"");
header("Content-Length: ".filesize($zipFilename));
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
header("Connection: Keep-Alive");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
header("Accept-Ranges: bytes");
readfile($zipFilename);
I am facing a problem of downloading script, whenever download on my project site starts, all links present in the site does not work at all.
Once file gets downloaded completely, link starts working.
I am setting header and using readfile method of php for download the file. I want that file downloading should work simultaneously with users actions on the site.
I am using following code
header("Pragma: public");
header("Expires: 0");
header("Content-Type: application/$ctype");
header("Content-Description: File Transfer");
header("Content-Disposition: inline; filename=$docName");
header('Content-Length: '. filesize($filename));
header("Accept Ranges: bytes");
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
readfile("$filename");
Here filename is name of file and ctype is file type.
How to resolve this?
Please let me know if anybody have solution for this.
I'm just guessing that you're missing a proper header, change content type and transfer encoding as you need
$download is the actual file i.e. /var/www/download_images/1.jpg
$filesize is the size of the file
$filename can be any filename that you want presented to the user i.e. homer_simpson.jpg
<?php
if (file_exists($download))
{
header("Cache-Control: must-revalidate");
header("Expires: 0");
header("Pragma: public");
header("Content-Description: File Transfer");
header("Content-Length: ". $filesize .";");
header("Content-Disposition: attachment; filename=" . $filename);
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
ob_clean();
flush();
readfile($download);
}
?>
I created a form, which, when a contained button is clicked, should open a download dialog to download a certain file. The file is placed on the same server.
I tried:
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=" . $file . '"');
Where $file is a local path + the file name, for example c:\mypath\myfile.xls. This does not work though. It offers me a file, but its not a valid file. How else could I do that?
Note: I wrote c:\ because its still on my local machine for testing.
Thanks!
Try 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/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));
die(file_get_contents($file));
I think file_get_contents() function is no longer work with PHP 5.0.3
Try this :
$path = "http://www.example.com/files/";
$filename = "abc.gif";
header("Content-Type:image/gif");
header("Content-Disposition: attachment; filename=".$filename);
header("Cache-control: private");
header('X-Sendfile: '.$path);
readfile($path);
exit;
PHP runs in server side, you can not download the files in clients machine.
Upload the files to server and then give that path for download.
Path must be refered from the site root...move the file
ex:
script path : C:/wamp/www/test.php
file C:/script.js
then:
if(file_exists('../../user.js'))
{
echo "OK";
}
Still a bad ideea..
I have the following code to push a zip file for download.
$filename = "ResourcePack_".time().".zip";
$destination = $basepath."downloads/$filename";
if($this->createdownload($files,$destination,false)){
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$destination").";");
header("Content-Disposition: attachment; filename='$filename'");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
ob_end_flush();
#readfile($destination);
if(file_exists($destination)){
unlink($destination);
}
}
I know the createdownload function is working to generate the zip file just fine because I see the file being created on the server. The problem is file is being written to the browser as a bunch of garbage instead of opening a download stream. Am I missing something in my headers?
EDIT
I was right. My problem is not with the php, but that calling the php file that generates this code via a JQuery $.ajax call is the problem. Using $.ajax automatically sets the Accept-Encoding request header to values incompatible with zip files. So, intead of using $.ajax I just used a simple window.open javascript command to call the same php page and it works just fine with the headers.
try to put a die after the #readfile
and remove the #, to see if you have any other error related with the file reading.
i have some code doing the same thing and this works for me:
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-Disposition: inline; filename="' . $filename . '"');
header('Content-type: application/zip');
//header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($destination));
readfile($destination);
die();
try passing proper type for that file. I think its fileinfo mime type see http://www.php.net/manual/en/ref.fileinfo.php
header("Content-Type: $file_type");
Also you have semicolon after octet-stream remove it
header("Content-type: application/octet-stream");
I would to know the command in a PHP script to get in output and save a file from my site.
Thanks
See here for a good description of how to force the output of a php script to be a download.
The basics of it are:
// Set headers
header("Cache-Control: public");
header("Content-Description: File Download");
header("Content-Disposition: attachment; filename=" + $filename);
header("Content-Type: application/zip"); // or whatever the mime-type is
// for the file you want to download
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($full_path_to_file);
As an addition (provided by Gordon's comment), see the 1st example on the php documentation here
At the End of the files or used in clicking files, you can add this
$filesh = "check.xls";
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($filesh));
header("Content-Description: File Transfer");
readfile($filesh);
if you got any header using problem means, top of the file you can add ob_start(); function
If you mean getting output, contents from other site or location, this what you need file_get_contents