Here we go.
With this Zip() function you can backup folder(s) in zip format...
[php]<?php
function Zip($source, $destination){
if(!extension_loaded('zip') || !file_exists($source)) {
return false; }
$zip = new ZipArchive();
if(!$zip->open($destination, ZIPARCHIVE::CREATE)) { return false; }
$source = str_replace('\\', '/', realpath($source));
if(is_dir($source) === true){
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); foreach($files as $file){
$file = str_replace('\\', '/', $file);
// Ignore "." and ".." folders
if(in_array(substr($file, strrpos($file, '/')+1), array('.', '..'))){ continue;
}
if(is_dir($file) === true)
{ $zip->addEmptyDir(str_replace($source . '/', '', $file)); } else if(is_file($file) === true){
$str1 = str_replace($source . '/', '', $file);
$zip->addFromString($str1, file_get_contents($file)); }}} else if(is_file($source) === true){ $zip->addFromString(basename($source), file_get_contents($source)); }
return $zip->close(); }
//usage
print Zip('foldername','savename.zip');
?>[/php]
hope it helps :D
PHP: Zip Folder On The Fly...
Info Post
0 comments:
Post a Comment