I have this code
<?php
$files = array('includes/s.xlsx', 'includes/test.html');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=filename.zip');
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
But i don’t want to make the zip file saved to the server i just want it to be served to the users.
ZIP is not a streamable format (the target needs to support seeking, i.e. a ZIP file cannot be simply written to a target where the previously-written data cannot be re-read and modified) what you are trying to do can’t work. So the best solution (if your zip files won’t be huge) is creating it in a temporary location,
readfile()it and then delete the temporary file.See the following Stack Overflow questions for further reference. They contain some workarounds that might be fine for you: