wesleybliss
3/13/2014 - 8:18 PM

Quick example of zipping a bunch of files in PHP.

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.';