Last active
August 29, 2015 13:57
-
-
Save wesleybliss/9536114 to your computer and use it in GitHub Desktop.
Quick example of zipping a bunch of files in 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
<?php | |
// Where the files will be added from | |
$path = './'; | |
$archiveName = 'test.zip'; | |
$zip = new ZipArchive; | |
$res = $zip->open( $archiveName, ZipArchive::CREATE ); | |
if ( $res !== TRUE ) { | |
exit( 'Error: could not create zip archive. ' . print_r(error_get_last()) ); | |
} | |
$dh = opendir( $path ) | |
or exit( 'Error: could not open directory' ); | |
while ( ($fn = @readdir($dh)) !== false ) { | |
if ( !in_array($fn, array('.', '..')) ) { | |
echo 'Adding ', $fn, PHP_EOL; | |
$zip->addFile( $path . $fn ); | |
} | |
} | |
@closedir( $dh ); | |
$zip->close(); | |
echo 'Zip archive created.'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment