Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:02
Show Gist options
  • Save code-boxx/1bb8ed6409e0a4b680cafade8941383d to your computer and use it in GitHub Desktop.
Save code-boxx/1bb8ed6409e0a4b680cafade8941383d to your computer and use it in GitHub Desktop.
PHP Add Files & Folders To Zip Archive

PHP ADD FILES & FOLDERS TO ZIP ARCHIVE

https://code-boxx.com/add-files-folders-php-zip/

NOTES

Please make sure that extension=zip is enabled in php.ini.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (A) NEW ZIP ARCHIVE OBJECT
$zip = new ZipArchive();
$zipfile = "demoA.zip";
// (B) OPEN/CREATE ZIP FILE
if ($zip->open($zipfile, ZipArchive::CREATE) === true) {
// (B1) ADD FILE
echo $zip->addFile("readme.txt", "filename-in-zip.txt")
? "File added to zip archive" : "Error adding file to zip archive" ;
// (B2) ADD FOLDER
echo $zip->addEmptyDir("folder")
? "Folder added to zip archive" : "Error adding folder to zip archive" ;
// (B3) CLOSE ZIP
echo $zip->close()
? "Zip archive closed" : "Error closing zip archive" ;
}
// (C) FAILED TO OPEN/CREATE ZIP FILE
else { echo "Failed to open/create $zipfile"; }
<?php
// (A) LOAD EXISTING ZIP ARCHIVE
$zip = new ZipArchive();
$zipfile = "demoA.zip";
if ($zip->open($zipfile, ZipArchive::CREATE) === true) {
// (B) APPEND FILE INTO ZIP
echo $zip->addFile("1-basic.php", "1-basic.php")
? "File added to zip archive" : "Error adding file to zip archive" ;
// (C) ADD FILE INTO FOLDER IN ZIP
echo $zip->addFile("2-append.php", "folder/2-append.php")
? "File added to zip archive" : "Error adding file to zip archive" ;
// (D) CLOSE ZIP
echo $zip->close()
? "Zip archive closed" : "Error closing zip archive" ;
}
// (E) FAILED TO OPEN/CREATE ZIP FILE
else { echo "Failed to open/create $zipfile"; }
<?php
// (A) OPEN/CREATE ZIP FILE
$zip = new ZipArchive();
$zipfile = "demoA.zip";
if ($zip->open($zipfile, ZipArchive::CREATE) === true) {
// (B) ADD FROM STRING
echo $zip->addFromString("string.txt", "This is a string!")
? "File added to zip archive" : "Error adding file to zip archive" ;
}
// (C) FAILED TO OPEN/CREATE ZIP FILE
else { echo "Failed to open/create $zipfile"; }
<?php
// (A) NEW ZIP ARCHIVE OBJECT
$zip = new ZipArchive();
$zipfile = "demoB.zip";
$tozip = __DIR__ . "/test/";
// (B) OPEN/CREATE ZIP FILE
if ($zip->open($zipfile, ZipArchive::CREATE) === true) {
// (B1) ADD ENTIRE FOLDER
// add all jpg png gif files in "/test/" into the "inside/" folder of the zip archive
echo $zip->addGlob($tozip . "*.{jpg,png,gif}", GLOB_BRACE, [
"add_path" => "inside/",
"remove_all_path" => true
]) ? "Folder added to zip" : "Error adding folder to zip" ;
// (B2) CLOSE ZIP
echo $zip->close()
? "Zip archive closed" : "Error closing zip archive" ;
}
// (C) FAILED TO OPEN/CREATE ZIP FILE
else { echo "Failed to open/create $zipfile"; }
<?php
// (A) NEW ZIP ARCHIVE OBJECT
$zip = new ZipArchive();
$zipfile = "demoC.zip";
$tozip = __DIR__ . "/test/";
// (B) OPEN/CREATE ZIP FILE
if ($zip->open($zipfile, ZipArchive::CREATE) === true) {
// (B1) ADD ENTIRE FOLDER
// add all php txt files in "/test/" into the "inside/" folder of the zip archive
echo $zip->addPattern("/\.(?:php|txt)$/", $tozip, [
"add_path" => "inside/",
"remove_all_path" => true
]) ? "Folder added to zip" : "Error adding folder to zip" ;
// (B2) CLOSE ZIP
echo $zip->close()
? "Zip archive closed" : "Error closing zip archive" ;
}
// (C) FAILED TO OPEN/CREATE ZIP FILE
else { echo "Failed to open/create $zipfile"; }
<?php
// (A) NEW ZIP ARCHIVE OBJECT
$zip = new ZipArchive();
$zipfile = "demoD.zip";
$tozip = __DIR__ . "/test/";
// (B) OPEN/CREATE ZIP FILE
if ($zip->open($zipfile, ZipArchive::OVERWRITE | ZipArchive::CREATE) === true) {
// (B1) RECURSIVE ADD
function zipall ($folder, $base, $ziparchive) {
// FOLDER NAME INSIDE ZIP
$options = ["remove_all_path" => true];
if ($folder != $base) {
$options["add_path"] = substr($folder, strlen($base));
}
// ADD CURRENT FOLDER TO ZIP ARCHIVE
echo $ziparchive->addGlob($folder."*.{jpg,png,gif}", GLOB_BRACE, $options)
? "Folder added to zip" : "Error adding folder to zip" ;
// ADD FOLDERS IN FOLDER
$folders = glob($folder . "*", GLOB_ONLYDIR);
if (count($folders)!=0) { foreach ($folders as $f) {
zipall($f."/", $base, $ziparchive);
}}
}
zipall($tozip, $tozip, $zip);
// (B2) CLOSE ZIP
echo $zip->close()
? "Zip archive closed" : "Error closing zip archive" ;
}
// (C) FAILED TO OPEN/CREATE ZIP FILE
else { echo "Failed to open/create $zipfile"; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment