This is what I use on my own site. My script pulls out a filtered list of file extensions from a directory.
First, you'll need to include zip.lib.php (here's the version I'm using: http://thefifthimperium.com/Random/zip.lib.php.txt, just save that file as zip.lib.php).
Below is the business end of the script I use to do the collection:
Obvious things you need to define - $zipdir, $ad_dir and $logdir, if you want it.
$suffix_array is a list of file extensions that save to the zip file:
$suffix_array = array ( '.htm', '.html', '.jpg', '.gif', '.opf' );
$about_message is just a variable containing, in my case, an 'About' message describing the collection. I build a small, but complete, HTML page in the variable and save it first to the zip file with a name that has a fairly good chance of listing first in the archive.
if ( !file_exists( $zipdir ) ) {
mkdir( $zipdir ); // Make ZIP storage directory, if needed
}
if ( !file_exists( $logdir ) ) { // if you intend to log things
mkdir( $logdir ); // Make log directory, if needed
}
if ( !file_exists( $zipdir . '/' . $zipfilename ) ) {
$zipfile = new zipfile(); // form is posted, handle it
$zipfile ->addFile( stripcslashes( $about_message ), $zip_subfolder . '/_about_this_file.htm' );
if ( $handle = opendir( $ad_dir ) ) {
while ( false !== ( $file = readdir( $handle ) ) ) {
if (!is_dir( $file ) && $file != "." && $file != "..") {
foreach ( $suffix_array as $suffix ) {
$len = strlen( $suffix );
if ( strtolower( substr( $file, -$len, $len ) == $suffix ) ) {
$f_tmp = @fopen( $ad_dir . '/' . $file, 'r');
if($f_tmp){
$dump_buffer=fread( $f_tmp, filesize( $ad_dir . '/' . $file ) );
$zipfile -> addFile( $dump_buffer, $zip_subfolder . '/' . $file );
fclose( $f_tmp );
}
}
}
}
}
$dump_buffer = $zipfile -> file();
}
// write the file to disk:
$file_pointer = fopen( $zipdir . '/' . $zipfilename, 'w' );
if( $file_pointer ){
fwrite( $file_pointer, $dump_buffer, strlen($dump_buffer) );
fclose( $file_pointer );
}
}
// log it, you can delete this bit, if you want to
$file_pointer = fopen( $logdir . '/' . $logname, 'a' );
if( $file_pointer ){
$logstr = '"' . $book . '","' . $format . '","' . $cd_name . '","' . $datestr . '","' . date('Y-m-d H:i:s.0T') . '","' . $_SERVER['REMOTE_ADDR'] . "\"\n";
fwrite( $file_pointer, $logstr );
fclose( $file_pointer );
}
My own script then feeds the new (or pre-existing) zip file out to the browser, so I continue on with this:
// response zip archive to browser:
header('Pragma: public');
header('Content-type: application/zip');
header('Content-length: ' . filesize($zipdir . '/' . $zipfilename));
header('Content-Disposition: attachment; filename="'.$zipfilename.'"');
readfile( $zipdir . '/' . $zipfilename );
htmlstartpage( $d_name, $badge );
echo <<<EOT
<center>
Thank you for downloading the <em>$format</em> version of <em>$book</em> in the file <em>$zipfilename</em>.
</center>
</body>
</html>
EOT;
exit;