Skip to content

Instantly share code, notes, and snippets.

@visabhishek
Last active May 3, 2018 09:48
Show Gist options
  • Save visabhishek/30946bfc7704400f55992ea192476d0b to your computer and use it in GitHub Desktop.
Save visabhishek/30946bfc7704400f55992ea192476d0b to your computer and use it in GitHub Desktop.
Backup File and Folders ( Zip and Backup), This will zip directory with all sub-directory and file given as source path.
<?php
ini_set('max_execution_time', 600);
ini_set('memory_limit', '1024M');
BackupzipData('/path/to/folder', '/path/to/backup.zip');
echo 'Backup Finished. Please download from /path/to/backup.zip';
function BackupzipData($source, $destination) {
if (extension_loaded('zip') === true) {
if (file_exists($source) === true) {
$zip = new ZipArchive();
if ($zip->open($destination, ZIPARCHIVE::CREATE) === true) {
$source = realpath($source);
if (is_dir($source) === true) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file = realpath($file);
if (is_dir($file) === true) {
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
else if (is_file($file) === true) {
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}
else if (is_file($source) === true) {
$zip->addFromString(basename($source), file_get_contents($source));
}
}
return $zip->close();
}
}
else {
print "Zip Extension not Loaded";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment