Skip to content

Instantly share code, notes, and snippets.

@wesleybliss
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesleybliss/9536114 to your computer and use it in GitHub Desktop.
Save wesleybliss/9536114 to your computer and use it in GitHub Desktop.
Quick example of zipping a bunch of files in PHP.
<?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