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条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况