Hi.
I have this program code which takes a zip-backup of a folder and all subfolders.
I need help on how to ignore 2 subfolders.
Any suggestions?
I have this program code which takes a zip-backup of a folder and all subfolders.
I need help on how to ignore 2 subfolders.
Any suggestions?
PHP:
<?php
Class ZipArchiver {
public static function zipDir($sourcePath, $outZipPath){
$pathInfo = pathinfo($sourcePath);
$parentPath = $pathInfo['dirname'];
$dirName = $pathInfo['basename'];
$z = new ZipArchive();
$z->open($outZipPath, ZipArchive::CREATE);
$z->addEmptyDir($dirName);
if($sourcePath == $dirName){
self::dirToZip($sourcePath, $z, 0);
}else{
self::dirToZip($sourcePath, $z, strlen("$parentPath/"));
}
$z->close();
return true;
}
private static function dirToZip($folder, &$zipFile, $exclusiveLength){
$handle = opendir($folder);
while(FALSE !== $f = readdir($handle)){
// Check for local/parent path or zipping file itself and skip
if($f != '.' && $f != '..' && $f != basename(__FILE__)){
$filePath = "$folder/$f";
// Remove prefix from file path before add to zip
$localPath = substr($filePath, $exclusiveLength);
if(is_file($filePath)){
$zipFile->addFile($filePath, $localPath);
}elseif(is_dir($filePath)){
// Add sub-directory
$zipFile->addEmptyDir($localPath);
self::dirToZip($filePath, $zipFile, $exclusiveLength);
}
}
}
closedir($handle);
}
}
$zipper = new ZipArchiver;
// Path of the directory to be zipped
$dirPath = dirname(__FILE__);
// Path of output zip file
$zipPath = 'backup-'.date('Y-m-d-His').'.zip';
// Create zip archive
$zip = $zipper->zipDir($dirPath, $zipPath);
if($zip){
echo 'ZIP archive created successfully.';
}else{
echo 'Failed to create ZIP.';
}
?>