How can I download multiple files as a zip-file using php?
You can use the ZipArchive
class to create a ZIP file and stream it to the client. Something like:
$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
and to stream it:
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
The second line forces the browser to present a download box to the user and prompts the name filename.zip. The third line is optional but certain (mainly older) browsers have issues in certain cases without the content size being specified.
$zip = new ZipArchive;
instead of $zip = new ZipFile;
? –
Sturdy readfile($zippath)
and filesize($zippath)
instead of "zipname' –
Squamous $zip->addFile($file);
to $zip->addFromString(basename($path), file_get_contents($path));
–
Octo time()
in the filename, as shown in @SunLove's example, it worked. –
Octo $zip->addFile($file,basename($file));
if you want to remove the paths. #3993605 –
Nf This is a working example of making ZIPs in PHP:
$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($files as $file) {
echo $path = "uploadpdf/".$file;
if(file_exists($path)){
$zip->addFromString(basename($path), file_get_contents($path));
}
else{
echo"file does not exist";
}
}
$zip->close();
Create a zip file, then download the file, by setting the header, read the zip contents and output the file.
http://www.php.net/manual/en/function.ziparchive-addfile.php
You are ready to do with php zip lib, and can use zend zip lib too,
<?PHP
// create object
$zip = new ZipArchive();
// open archive
if ($zip->open('app-0.09.zip') !== TRUE) {
die ("Could not open archive");
}
// get number of files in archive
$numFiles = $zip->numFiles;
// iterate over file list
// print details of each file
for ($x=0; $x<$numFiles; $x++) {
$file = $zip->statIndex($x);
printf("%s (%d bytes)", $file['name'], $file['size']);
print "
";
}
// close archive
$zip->close();
?>
http://devzone.zend.com/985/dynamically-creating-compressed-zip-archives-with-php/
and there is also php pear lib for this http://www.php.net/manual/en/class.ziparchive.php
I open that zip file occur this popup An error occurred while loading the archive. give solution MY code
if (isset($_POST['selectedFiles'])) {
// Handle form submission
$selectedFiles = $_POST['selectedFiles'];
$zip = new ZipArchive();
$error = "";
$zipname = 'selected_files.zip';
$zip->open($zipname, ZipArchive::CREATE);
foreach ($selectedFiles as $fileName) {
$filePath = realpath('..').'/QUERY_FILE/'. $fileName;
if (file_exists($filePath)) {
$added = $zip->addFile($filePath, $fileName);
if (!$added) {
$error .= "* Failed to add file to ZIP: $fileName\n";
}
} else {
$error .= "* File not found: $fileName\n";
}
}
$zip->close();
if (file_exists($zipname)) {
// Download the zip file
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
// Delete the zip file after download
// unlink($zipname);
}
if (!empty($error)) {
echo "Errors occurred while creating the ZIP file:<br>" . $error;
}
} else {
echo 'No files selected';
}
© 2022 - 2024 — McMap. All rights reserved.