Last active
May 5, 2023 07:27
-
-
Save vielhuber/de2187cab0e74afa476c to your computer and use it in GitHub Desktop.
zip files on the fly #php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Helped me after an hour of searching. Thanks