Skip to content

Instantly share code, notes, and snippets.

@usainicola
Last active May 21, 2019 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usainicola/a1567b5af56367656c49645cbf774e27 to your computer and use it in GitHub Desktop.
Save usainicola/a1567b5af56367656c49645cbf774e27 to your computer and use it in GitHub Desktop.
php backup zip current folder
<pre>
<?php
$path = realpath(__DIR__);
echo "<p>Zipping...</p>";
$zip = new ZipArchive();
$zip->open(dirname(__FILE__).'.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
foreach ($files as $name => $file) {
if ($file->isDir()) {
echo $name . "\r\n";
flush();
continue;
}
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($path) + 1);
$zip->addFile($filePath, $relativePath);
}
if ($zip->close()) {
echo "Done\r\n";
} else {
echo "Error\r\n";
}
?>
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment