2013-07-18 48 views
1
$path = '/home/username/www/; 

if($zip = new ZipArchive){ 
    if($zip->open('backup_'. time() .'.zip', ZipArchive::CREATE)){ 
     if(false !== ($dir = opendir($path))){ 
      while (false !== ($file = readdir($dir))){ 
       if ($file != '.' && $file != '..' && $file != 'aaa'){ 
        $zip->addFile($path . $file); 
        echo 'Adding '. $file .' to path '. $path . $file .' <br>'; 
       } 
      } 
     } 
     else 
     { 
      echo 'Can not read dir'; 
     } 

     $zip->close(); 
    } 
    else 
    { 
     echo 'Could not create backup file'; 
    } 
} 
else 
{ 
    echo 'Could not launch the ZIP libary. Did you install it?'; 
} 

Hello again Stackoverflow!我想备份一个文件夹及其所有内容,包括(空)子文件夹和其中的每个文件,同时排除一个文件夹(当然...)。需要排除的文件夹是aaa创建一个ZIP备份文件 - 没有错误抛出,但ZIP文件没有显示

所以,当我运行这个脚本(每个文件夹确实有chmod 0777)它运行没有错误,但ZIP文件不显示。为什么?我该如何解决这个问题?

在此先感谢!

+0

对于那些想知道的,是的,我在这里搜索了Stackoverflow的答案,但没有任何帮助。 – Thew

+0

我有这个确切的问题。你有没有想过它? – David

+0

@David对不起,我没有显示我的答案,我现在就把它放在这里。 – Thew

回答

0
function addFolderToZip($dir, $zipArchive, $zipdir = ''){ 
     if (is_dir($dir)) { 
      if ($dh = opendir($dir)) { 

       //Add the directory 
       if(!empty($zipdir)) $zipArchive->addEmptyDir($zipdir); 

       // Loop through all the files 
       while (($file = readdir($dh)) !== false) { 

        //If it's a folder, run the function again! 
        if(!is_file($dir . $file)){ 
         // Skip parent and root directories, and any other directories you want 
         if(($file !== ".") && ($file !== "..") && ($file !== "aa")){ 
          addFolderToZip($dir . $file . "/", $zipArchive, $zipdir . $file . "/"); 
         } 

        }else{ 
         // Add the files 
         $zipArchive->addFile($dir . $file, $zipdir . $file); 

        } 
       } 
      } 
     } 
    } 

一会儿鬼混后这是我发现的工作。如下所示使用它。

$zipArchive = new ZipArchive; 

$name = 'backups\backup_'. time() .'.zip'; 

$zipArchive->open($name, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE); 

addFolderToZip($path, $zipArchive); 
1

你试图访问通过PHP的zip文件夹,而不是在FTP看它是否存在与否 - 因为它可能不会立即出现,以查看FTP

+0

是的,找不到。 – Thew

0

这是我的答案,检查修改时间是否大于某种情况。

<?php 

$zip = new ZipArchive; 

$zip_name = md5("backup".time()).".zip"; 

$res = $zip->open($zip_name, ZipArchive::CREATE); 

$realpath = str_replace('filelist.php','',__FILE__); 

$path = realpath('.'); 
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); 
foreach($objects as $name => $object){ 
     if (is_file($object)) { 
      $file_count ++; 
      $epoch = $object->getMTime(); 
      if($epoch>='1374809360'){ // Whatever date you want to start at 
       $array[] = str_replace($realpath,'',$object->getPathname());  
      } 

     } 
} 


    foreach($array as $files) { 
     $zip->addFile($files); 
    }  

    $zip->close(); 

    echo $zip_name.'-'.$file_count.'-'.$count_files; 
?>