Last active
May 21, 2019 02:10
-
-
Save usainicola/a1567b5af56367656c49645cbf774e27 to your computer and use it in GitHub Desktop.
php backup zip current folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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