Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This script will recursively create a zip file.
- The only thing you have to do is give the function a source and destination like so:
- $zip = createZip($source, $destination);
- ==== SCRIPT HERE =====
- <?php
- /*
- * NOTE: Comments between stars don't have anything to do with what the code actually does.
- * They are merely for funzies!
- * */
- /* Start the dragon hunt. */
- function createZip($source, $destination){
- /* Did we find a dragon?? yes!? */
- //cheack wether the zip extension is installed and the source exists.
- if (!extension_loaded('zip') || !file_exists($source)) {
- return false; /* No dragons here, move on lads! */
- }
- /* Quickly grab a container to put the dragon in! */
- //Create a new zip archive
- $zip = new ZipArchive();
- /* Does the container have a lock??? Why didn't you remove the damn lock!? */
- //check wether the zip is open and created.
- if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
- return false;
- }
- /* Did we find mum, or a baby dragon? */
- //check if source is a dir, not a file
- if (is_dir($source) === true){
- $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
- foreach ($files as $file){/* Ayy, matey it's a mommy dragon quick search it's pouches! */
- $file = str_replace('\\', '/', $file);
- /* Remember! We don't need the eggs! */
- // Ignore unix hidden folders
- if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
- continue;
- /* This baby dragon has babies??? or not?? */
- //check wether the "file" is actually a folder, if it is add an empty dir to the zip.
- if (is_dir($file) === true){/* Just bag the baby with the babies anyways */
- $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
- }
- /* Oh, good it's just a baby dragon... */
- //if it turns out to be a file, just add it :D
- else if (is_file($file) === true){
- $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
- }
- }
- }
- /* Doesn't matter, bag the baby anyways */
- else if (is_file($source) === true){// if it IS a file, zip it.
- $zip->addFromString(basename($source), file_get_contents($source));
- }
- /* Let's bring the container to the king! */
- //close the zip and return it
- return $zip->close();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment