Skip to content

Instantly share code, notes, and snippets.

@dgoze
Forked from vovadocent/add_files_to_zip.php
Created August 15, 2024 23:27
Show Gist options
  • Save dgoze/19bd0cf0c79dc5ee67a96228e7c3cd0d to your computer and use it in GitHub Desktop.
Save dgoze/19bd0cf0c79dc5ee67a96228e7c3cd0d to your computer and use it in GitHub Desktop.
Add Files to Zip
<?php
$files = array('file1.txt', 'file2.csv', 'file3.png');
$zip = new ZipArchive();
$zip_name = "out/" . time() . ".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($files as $path) {
if (file_exists($path)) {
$zip->addFromString(basename($path), file_get_contents($path));
} else {
echo"file does not exist";
}
}
$zip->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment