Skip to content

Instantly share code, notes, and snippets.

@doyomay
Forked from menzerath/backup.php
Created February 18, 2021 11:32
Show Gist options
  • Select an option

  • Save doyomay/19bbcd3d8fd3cc977159ca9c64382201 to your computer and use it in GitHub Desktop.

Select an option

Save doyomay/19bbcd3d8fd3cc977159ca9c64382201 to your computer and use it in GitHub Desktop.
PHP: Recursively Backup Files & Folders to ZIP-File
<?php
/*
* PHP Recursive Backup-Script to ZIP-File
* (c) 2012: Marvin Menzerath. (http://menzerath.eu)
*/
// Make sure the script can handle large folders/files
ini_set('max_execution_time', 600);
ini_set('memory_limit','1024M');
// Start the backup!
zipData('/path/to/folder', '/path/to/backup_'.date("d.m.Y-H.i").'.zip');
echo date("d.m.Y-H.i").' - Abgeschlossen.';
// Here the magic happens :)
function zipData($folder, $zipTo) {
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();
}
}
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment