Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active May 5, 2023 07:27
Show Gist options
  • Save vielhuber/de2187cab0e74afa476c to your computer and use it in GitHub Desktop.
Save vielhuber/de2187cab0e74afa476c to your computer and use it in GitHub Desktop.
zip files on the fly #php
$tmpfile = tempnam("tmp", "zip");
$zip = new ZipArchive();
$zip->open($tmpfile, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$files = [[$_SERVER['DOCUMENT_ROOT'].'/.../foo.txt','foo.txt'],[$_SERVER['DOCUMENT_ROOT'].'/.../bar.txt','bar.txt']];
foreach($files as $files__value) {
$zip->addFile(($_SERVER['DOCUMENT_ROOT'].'/.../'.$files__value[0]), $files__value[1]);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($tmpfile));
header('Content-Disposition: attachment; filename="file.zip"');
readfile($tmpfile);
unlink($tmpfile);
die();
@ArseniyKruglov
Copy link

Helped me after an hour of searching. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment