This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to zip a whole folder using PHP
Hi,
I am creating a zip folder using zipArchive in php.The zipped file contains a folder(with a file in it) and a file. I am using the following code to download the zipped file:
$file_path = "zip/test.zip";
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=download.zip');
header('Content-Length: ' . filesize( $file_path));
readfile($file_path);
The folder gets downloaded. But I am unable to unzip it.
The error specified is "Decompression failed".
I can download a single file with the same code but not a file within a folder.
Please help..
Here is the code to zip the files:
$zip = new ZipArchive;
if ($zip->open($file_path,ZIPARCHIVE::CREATE) === TRUE) {
if($zip->addEmptyDir('dir1')) {
$zip->addFile($filepath,$destinationPath);
if($zip->addEmptyDir('Files')) {
$zip->addFile($filepath2,$destinationPath2);
}
}
}
I got the code working...
Need to add a $zip->close() code.
Related
I have created a jpeg-pdf conversion tool in php. After the conversion I have multiple pdf files. So I created a $zip in php to store the pdf files so that user can download all the single pdf files at once.
In my linux system when I'm downloading the zip file that is getting generated I cant open the individual pdf files from the zip showing "error opening file: some/path/: Permission Denied". So I even tried assigning 777 permission to the zip as you can see below which also didn't workout.
But the pdf files inside the zip is opening fine if I open it after extracting the zip.
Here is my snippet where I am creating the zip.
Please note I'm using headers to download the zip. Is that the cause of this problem ?
for ($x = $arrayEndIndex; $x >= 0; $x--) {
$pdf = new Imagick($imageFilesArray[$x]);
$pdf->setImageFormat('pdf');
$pdf->writeImages("../upload/converted-{$x}.pdf", true);
//chmod("../upload/converted-{$x}.pdf", 0777);
array_push($pdfFilesArray,"../upload/converted-{$x}.pdf");
}
$zip = new ZipArchive();
$tmp_file = tempnam('.', '');
chmod($tmp_file, 0777);
$zip->open($tmp_file, ZipArchive::CREATE);
foreach ($pdfFilesArray as $pdf) {
$download_file = file_get_contents($pdf);
$zip->addFromString(basename($pdf), $download_file);
}
$zip->close();
header('Content-disposition: attachment; filename="Splitted-PDFs.zip"');
header('Content-type: application/zip');
readfile($tmp_file);
unlink($tmp_file);
I am using ZipArchive with the following code to create zip files. It works well in all browsers on a Mac. But it says the file is invalid on any browser on a Windows computer. I don't understand why. I emailed the supposedly corrupt file from the Windows computer to myself and opened it on my Mac computer, and it worked fine. I also read through all the suggestions on this thread and tried all of them, with no luck.
Do you see anything wrong with my code?
if(extension_loaded('zip')){
if(isset($post['afiles']) and count($post['afiles']) > 0){
$zip = new ZipArchive();
$url = get_stylesheet_directory();
$filepath = $url;
$zip_name = "DSV".time().".zip";
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
$error .= "Error";
}
foreach($post['afiles'] as $file){
// get the file directory url from the file ID
$path = get_attached_file( $file );
// add each file to the zip file
$zip->addFile($path, basename($path));
}
$zip->close();
ob_clean();
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
unlink($zip_name);
}else
$error .= "* Please select file to zip <br/><br />";
}else
$error .= "* You do not have the ZIP extension<br/><br />";
I echoed basename($path) to confirm that there are no slashes. It is simply the filename like "this-is-my-file.docx". Thanks for any insight!
Edit: The exact error in Windows says:
Compressed (zipped) Folders Error
Windows cannot open the folder.
The Compressed (zipped) Folder 'C:\Users\krist\Downloads\DWV1620652983.zip' is invalid.
After inspecting the ZIP files, there's HTML coming after the ZIP content. The fix is to make sure to call exit as soon as possible after calling readfile so that nothing else is written to the stream.
I have the below code that creates a ZIP file, adds a file to it and then downloads to my computer.
$zip = new ZipArchive();
if ($zip->open('order_sheets.zip', ZipArchive::CREATE) === TRUE){
$zip->addFile($pdfFilePath);
}
$zip->close();
$file_url = 'order_sheets.zip';
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$file_url);
header('Content-Length: ' . filesize($file_url));
readfile($file_url);
All works good but the issue is, when opening the downloaded ZIP, it say's This folder is empty when it is not. If i right click and hit "Extract Here", the contents come out.
Anyone know why that is?
The problem is with Windows' zip utility, which uses IBM850 encoding causing it to misinterpret some characters in internal filenames in the archive, including _ (underscore) as in your file.
See answer here:
PHP ZipArchive Corrupt in Windows
Explained in PHP Manual user notes here: http://php.net/manual/en/ziparchive.addfile.php#95725
I'm using the PHP Flysystem package to stream content from my Amazon S3 bucket. In particular, I'm using $filesystem->readStream.
My Question
When I stream a file, it ends up in myzip.zip and the size is correct, but when unzip it, it become myzip.zip.cpgz. Here is my prototype:
header('Pragma: no-cache');
header('Content-Description: File Download');
header('Content-disposition: attachment; filename="myZip.zip"');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
$s3 = Storage::disk('s3'); // Laravel Syntax
echo $s3->readStream('directory/file.jpg');
What am I doing wrong?
Side Question
When I stream a file like this, does it:
get fully downloaded into my server's RAM, then get transferred to the client, or
does it get saved - in chunks - in the buffer, and then get transferred to the client?
Basically, is my server being burdened if I have have dozens of GB's of data being streamed?
You are currently dumping the raw contents of the directory/file.jpg as the zip (which a jpg is not a zip) . You need to create a zip file with those contents.
Instead of
echo $s3->readStream('directory/file.jpg');
Try the following in its place using the Zip extension:
// use a temporary file to store the Zip file
$zipFile = tmpfile();
$zipPath = stream_get_meta_data($zipFile)['uri'];
$jpgFile = tmpfile();
$jpgPath = stream_get_meta_data($jpgFile)['uri'];
// Download the file to disk
stream_copy_to_stream($s3->readStream('directory/file.jpg'), $jpgFile);
// Create the zip file with the file and its contents
$zip = new ZipArchive();
$zip->open($zipPath);
$zip->addFile($jpgPath, 'file.jpg');
$zip->close();
// export the contents of the zip
readfile($zipPath);
Using tmpfile and stream_copy_to_stream, it will download it in chunks to a temporary file on disk and not into RAM
I am trying to download an android APK file using php in my PC browser using Chrome.
My app is located in a particular path in the server. If I manually FTP the file from server and transfer to my android mobile it installed perfect. But when I downloaded using PHP and transfer the downloaded file to Mobile and while installing it throws 'there was a problem while parsing the package'.
Here is my PHP code I use to download
header('Content-Type: application/vnd.android.package-archive');
header('Content-Disposition: attachment; filename="' . $file_name . '"');
readfile($file_path);
return true;
fyi...
$file_name is the apk file file 'myfile.apk'
$file_path is the full absolute path of the file in server ('d:\abcd\xyz\xampp\htdocs\apkstore\myfile.apk')
I found one observation while trying to open the APK file using 7-zip.
When I open the file using 7-zip it throws an error 'Cannot open the file xxx as archive'
After I added the below PHP code
header("Content-length: " . filesize($file_path));
Now when I open the file using 7-zip it opens the file but the size of the downloaded file is greater than the original file. And when I open this file in mobile the same error 'there was a problem while parsing the package'
To cut my long story short, I am trying to download an APK file from server to localhost using PHP and I'm able to make it work.
I managed to make it work by adding ob_end_flush()
header('Content-Type: application/vnd.android.package-archive');
header("Content-length: " . filesize($file_path));
header('Content-Disposition: attachment; filename="' . $file_name . '"');
ob_end_flush();
readfile($file_path);
return true;