PHP online zip compression function code
- /* Create a zip package in PHP */
- Function create_zip ($ files = array (), $ destination = '', $ overwrite = false ){
- // If the zip file already exists and overwrite is false, return false
- If (file_exists ($ destination )&&! $ Overwrite) {return false ;}
- // Vars
- $ Valid_files = array ();
- // If files were passed in...
- If (is_array ($ files )){
- // Cycle through each file
- Foreach ($ files as $ file ){
- // Make sure the file exists
- If (file_exists ($ file )){
- $ Valid_files [] = $ file;
- }
- }
- }
- // If we have good files...
- If (count ($ valid_files )){
- // Create the archive
- $ Zip = new ZipArchive ();
- If ($ zip-> open ($ destination, $ overwrite? ZIPARCHIVE: OVERWRITE: ZIPARCHIVE: CREATE )! = True ){
- Return false;
- }
- // Add the files
- Foreach ($ valid_files as $ file ){
- $ Zip-> addFile ($ file, $ file );
- }
- // Debug
- // Echo The zip archive ins, $ zip-> numFiles, 'files with a status of ', $ zip-> status;
- // Close the zip -- done!
- $ Zip-> close ();
- // Check to make sure the file exists
- Return file_exists ($ destination );
- }
- Else
- {
- Return false;
- }
- }
- /***** Application instance ***/
- $ Files = array ('1. php', 'mail. php', 'readme.txt ');
- Create_zip ($ files, 'myzipfile.zip ', true );
- ?>
|