Mastermindzh

Php zip function

Nov 9th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. This script will recursively create a zip file.
  2. The only thing you have to do is give the function a source and destination like so:
  3. $zip = createZip($source, $destination);
  4.  
  5.  
  6. ==== SCRIPT HERE =====
  7. <?php
  8. /*
  9.  * NOTE: Comments between stars don't have anything to do with what the code actually does.
  10.  *       They are merely for funzies!
  11.  * */
  12.  
  13.     /* Start the dragon hunt. */
  14.     function createZip($source, $destination){
  15.         /* Did we find a dragon?? yes!? */
  16.         //cheack wether the zip extension is installed and the source exists.
  17.         if (!extension_loaded('zip') || !file_exists($source)) {
  18.             return false; /* No dragons here, move on lads! */
  19.         }
  20.         /* Quickly grab a container to put the dragon in! */
  21.         //Create a new zip archive
  22.         $zip = new ZipArchive();
  23.        
  24.         /* Does the container have a lock??? Why didn't you remove the damn lock!? */
  25.         //check wether the zip is open and created.
  26.         if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
  27.             return false;
  28.         }
  29.        
  30.         /* Did we find mum, or a baby dragon? */
  31.         //check if source is a dir, not a file
  32.         if (is_dir($source) === true){
  33.             $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
  34.             foreach ($files as $file){/* Ayy, matey it's a mommy dragon quick search it's pouches! */
  35.                 $file = str_replace('\\', '/', $file);
  36.                 /* Remember! We don't need the eggs! */
  37.                 // Ignore unix hidden folders
  38.                 if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
  39.                     continue;
  40.                     /* This baby dragon has babies??? or not?? */
  41.                 //check wether the "file" is actually a folder, if it is add an empty dir to the zip.
  42.                 if (is_dir($file) === true){/* Just bag the baby with the babies anyways */
  43.                     $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
  44.                 }
  45.                 /* Oh, good it's just a baby dragon... */
  46.                 //if it turns out to be a file, just add it :D
  47.                 else if (is_file($file) === true){
  48.                     $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
  49.                 }
  50.             }
  51.         }
  52.         /* Doesn't matter, bag the baby anyways */
  53.         else if (is_file($source) === true){// if it IS a file, zip it.
  54.             $zip->addFromString(basename($source), file_get_contents($source));
  55.         }
  56.         /* Let's bring the container to the king! */
  57.         //close the zip and return it
  58.         return $zip->close();
  59.     }
  60. ?>
Advertisement
Add Comment
Please, Sign In to add comment