note 68074 deleted from ref.zip by danbrown

From: Date: Sun, 25 Oct 2009 13:11:30 +0000
Subject: note 68074 deleted from ref.zip by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162059@lists.php.net to get a copy of this message
Note Submitter: Lennart Poot ---- You can use PHPMyAdmin's zip.lib library. It's available in the package PHPMyAdmin ( http://www.phpmyadmin.net/ ). The zip class is located in /libraries/zip.lib.php Next example generates a file named 'subfolder_in_zip_archive/_settings.php' using information of variable $settings. All files in folder 'folder_holding_files_to_add_to_zip' ($ad_dir) will be added to the archive as well. In this example the raw zip archive data is responded to the browser. Uncomment the lines after 'write the file to disk' if you would like the archive to be stored on disk. The function $zipfile -> addFile requires two parameters. - The first parameter holds raw data and needs to be escaped using php function ‘stripcslashes’ - The second parameter holds the filename in the archive. You can enter complete paths to create complex folder structures A timestamp can be added using the third additional parameter. Regars, Lennart Poot <?php require_once('zip.lib.php'); $zipfilename = 'zip_archive_to_generate.zip'; $zip_subfolder = 'subfolder_in_zip_archive'; $settings = ' <?php $setting1 = "'.$setting1.'"; $setting2 = "'.$setting2.'"; ?> '; $ad_dir = 'folder_holding_files_to_add_to_zip'; // form is posted, handle it $zipfile = new zipfile(); // generate _settings into zip file $zipfile ->addFile( stripcslashes( $settings ), $zip_subfolder . '/_settings.php' ); if ($handle = opendir($ad_dir)) { while (false !== ($file = readdir($handle))) { if (!is_dir($file) && $file != "." && $file != ".." ) { $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('newzip.zip', 'w'); if($file_pointer){ fwrite( $file_pointer, $dump_buffer, strlen($dump_buffer) ); fclose( $file_pointer ); } */ // response zip archive to browser: header('Pragma: public'); header('Content-type: application/zip'); header('Content-length: ' . strlen($dump_buffer)); header('Content-Disposition: attachment; filename="'.$zipfilename.'"'); exit; ?>

« previous php.notes (#162059) next »