Skip to content

Instantly share code, notes, and snippets.

@toddsby
Forked from menzerath/backup.php
Last active December 30, 2021 06:01
Show Gist options
  • Save toddsby/f98d82314259ec5483d8 to your computer and use it in GitHub Desktop.
Save toddsby/f98d82314259ec5483d8 to your computer and use it in GitHub Desktop.
backup.php
<?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');
// Start the backup!
zipData('/path/to/folder', '/path/to/backup.zip');
echo 'Finished.';
// Here the magic happens :)
function zipData($source, $destination) {
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) {
$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;
}
@djvov
Copy link

djvov commented Dec 10, 2014

great!
how I can to exclude some folders from archiving?

@Firestorm-Graphics
Copy link

would love to know how to exclude folders also

@manojiksula
Copy link

how can we skip initial folder we only need current folder current it is creating all folders from the root directory.

@thameerakn
Copy link

Great script. But when create a zip it will not create the base directory. How to create the base directory in the script?

@callmewilliams
Copy link

Use this for windows to exclude root directories

function zipData($source, $destination) { 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) { $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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment