Downloading corrupt .zip (php) - php

binaryhere is my problem
I'm working with php, I create a zip archive thx to ZipArchive library.
I complete it with all files I need and save it on my server.
Then I want to make it download by the current user, here is what I wrote :
if (file_exists($zip_archive))
{
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=\"".$zip_archive."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($zip_archive));
ob_end_flush();
readfile($zip_archive);
}
But when I download the zip archive, I can see files inside, but when I try to extract them, an error occurs telling me that the file might be corrupted.
So I tried to open the zip archive saved on the server, and NO error appears.
Have you got any idea to help me ?
Thanks for any help.
(I changed fichier to binary, but a problem persists : End of archive ...)

This line:
header("Content-Transfer-Encoding: fichier");
Should read:
header("Content-Transfer-Encoding: binary");
You can read more about this header at http://www.w3.org/Protocols/rfc1341/5_Content-Transfer-Encoding.html. There is also a list of available encoding types, suffice to say 'fichier' isn't there.

Related

Generated Excel file is downloaded as XML instead

I'm generating and using the following sample XML to generate and download an XLSX file.
<!--?xml version="1.0"?-->
<!--?mso-application progid="Excel.Sheet"?-->
<ss:workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<ss:worksheet ss:name="Title">
<ss:table>
<ss:column>
<ss:row>
<ss:cell><ss:data ss:type="String">Something</ss:data></ss:cell>
<ss:cell><ss:data ss:type="String">Something</ss:data></ss:cell>
</ss:row>
</ss:column>
</ss:table>
</ss:worksheet>
</ss:workbook>
I then use the following code snippet to force the file to download.
<?php
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-Type: application/octet-stream");
header("Content-Type: application/download");
;
header("Content-Disposition: attachment;filename=something.xml ");
header("Content-Transfer-Encoding: binary ");
print $this->file;
?>
The file downloads correctly; however, the extension is XML. I can manually open it in Excel and it would show correctly. I've tried changing the filename in the Content-Disposition line to something.xlsx, but when I try to open the file by double-clicking on it, I get a warning that:
Excel cannot open the file something.xlsx because the file format or
file extension is not valid. Verify that the file has not been
corrupted and that the file extension matches the format of the file.
Any idea what I'm doing wrong? Is the XML not in the right format?
You only need to set the Content-Type once, and you should set it to the correct value:
<?php
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=something.xlsx");
header("Content-Transfer-Encoding: binary");
echo $this->file;
Also best to leave off trailing ?> so that no stray whitespace gets into the downloaded file.

Facing issue of downloading script in PHP

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

PHP Download Script Creates Unreadable ZIP File on Mac

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

Force PRS-T1 browser to download file with .htaccess-authentification

I have a php-download script that looks like this:
<?php
header("Pragma: public"); // required
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-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Content-Type: $mimeType");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $size");
// Read the file from disk
readfile($path);
?>
Normally with this I can force my E-Book-Reader (a Sony PRS-T1) to download a file. However, if I use the same script inside of a folder with .htaccess-authentication, the download fails.
On my computer, the download works, no matter if there is a .htaccess-authentication or not.
Can you help me to find the reason for this behaviour of my Ebook-Reader and make downloads inside of protected folders possible?
Thank you!
[edit]
The PRS-T1 runs Android. Maybe this issue (link) is the answer - it is simply not possible...?

Download file from site PHP

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

Categories