Hey there,
i am knocking my brain out here …
I am trying to generate a zip file based on image paths coming from a url parameter.
Ended up using the zipArchive standard like this way: https://itsolutionstuff.com/post/php-how-to-create-zip-file-and-download-using-ziparchive-example.html.
Also tried this method https://processwire.com/api/ref/wire-file-tools/zip/ before … UNF without any luck on both methods.
All I am getting is a zip file with zero bytes. No errors appear. I think the file isn't even been created in the first place and it is just downloading the defined zip file coming from $fileName
Here is my code:
<?php
$lb_images = "[".htmlentities($_GET['images'])."]";
// $lb_images = ['/site/assets/files/1062/image-1.jpg','/site/assets/files/1062/image-2.jpg','/site/assets/files/1062/image-3.jpg', ...]
function createZip($files_ = array(), $destination = '', $overwrite = false) {
if(file_exists($destination) && !$overwrite) { return false; }
$validFiles = [];
if(is_array($files_)) {
foreach($files_ as $file) {
if(file_exists($file)) {
$validFiles[] = $file;
}
}
}
if(count($validFiles)) {
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
foreach($validFiles as $file) {
$zip->addFile($file,$file);
}
$zip->close();
return file_exists($destination);
}else{
return false;
}
}
$fileName = 'my_zip_'.time().'.zip';
$files_to_zip = $lb_images;
$result = createZip($files_to_zip, $fileName);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $fileName);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fileName));
ob_clean();
flush();
readfile($fileName);
exit;
?>