Here are some good answers. Here is mine so you can choose the best for you.
I am just adding a new var $exclude , you can set the folders and files you want to exclude. For instance. You want to exclude folder 3, 7 and file 8 / fdsgdfg.js:
<?php /* * PHP: Recursively Backup Files & Folders to ZIP-File * (c) 2012-2014: Marvin Menzerath - http://menzerath.eu * contribution: Drew Toddsby */ // Make sure the script can handle large folders/files ini_set('max_execution_time', 600); ini_set('memory_limit','1024M'); $dir = '/var/www/hack/to_bk'; $exclude = array("$dir/3","$dir/7", "$dir/8/fdsgdfg.js"); // Start the backup! zipData($dir, '/var/www/html/uploaded.zip', $exclude); echo 'Finished.'; // Here the magic happens :) function zipData($source, $destination, $exclude) { if (extension_loaded('zip')) { if (file_exists($source)) { $zip = new ZipArchive(); if ($zip->open($destination, ZIPARCHIVE::CREATE)) { $source = realpath($source); if (is_dir($source)) { $iterator = new RecursiveDirectoryIterator($source); // skip dot files while iterating $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS); $files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST); foreach ($files as $file) { if ( in_array($file, $exclude) ) { continue; } if ( is_file($file) ) { $p = pathinfo($file); if ( in_array($p['dirname'], $exclude) ) { continue; } } $file = realpath($file); if (is_dir($file)) { $zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); } else if (is_file($file)) { $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); } } } else if (is_file($source)) { $zip->addFromString(basename($source), file_get_contents($source)); } } return $zip->close(); } } return false; }
if the current file / folder is in the array, and then just continue, do not execute the code if its file allows you to check whether this folder file is in the exclude array.
No additional functions, one more parameter, and there =)
source share