doubi8383 2013-07-18 10:58
浏览 27
已采纳

创建ZIP备份文件 - 未抛出任何错误,但ZIP文件未显示

$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! I want to backup a folder with all its content including (empty) subfolders and every file in them, whilst excluding a single folder (and ofcourse . and ..). The folder that needs to be excluded is aaa.

So when I run this script (every folder does have chmod 0777) it runs without errors, but the ZIP file doesn't show up. Why? And how can I solve this?

Thanks in advance!

  • 写回答

3条回答 默认 最新

  • doulvyi2172 2013-07-26 10:30
    关注
    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); 
    
                        } 
                    } 
                } 
            } 
        }
    

    After a while of fooling around this is what I found working. Use it as seen below.

    $zipArchive = new ZipArchive;   
    
    $name = 'backups\backup_'. time() .'.zip';
    
    $zipArchive->open($name, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
    
    addFolderToZip($path, $zipArchive);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)