If for backup or any other purpose, you want to zip an entire folder then this script is for you are searching for. You need not change even a single character in this script. Just execute this script in the folder you want to backup. A backup zip file will be created.
If for any reason, you do not wish to include this script in backup archive, then place this script in the root and write path of the folder you want to backup ( do not add slash(/) at the end of the path )
<?php
// requirements: php zip extensions with ZipArchive class
$directory = '.'; // you can change this if you want to take
// backup of any particular folder only
date_default_timezone_set('Asia/Calcutta');
// you can set your timezone or can delete above line.
$stamp = date('Y-m-d-H-i-s');
$zipfile = "backup_".$stamp.".zip";
$filenames = array();
function browse($dir) {
global $filenames;
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && is_file($dir.'/'.$file)) {
$filenames[] = $dir.'/'.$file;
}
else if ($file != "." && $file != ".." && is_dir($dir.'/'.$file)) {
browse($dir.'/'.$file);
}
}
closedir($handle);
}
return $filenames;
}
browse($directory);
$zip = new ZipArchive();
if ($zip->open($zipfile, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$zipfile>\n");
}
foreach ($filenames as $filename) {
echo "Adding " . $filename . "<br/>";
$zip->addFile($filename,$filename);
}
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
?>