Creating a ZIP backup file - No errors thrown, but the ZIP file is not showing up

StackOverflow https://stackoverflow.com/questions/17721570

  •  03-06-2022
  •  | 
  •  

سؤال

$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!

هل كانت مفيدة؟

المحلول 2

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

نصائح أخرى

have you tried to access the zip folder via PHP rather than looking in FTP as to whether it exists or not - as it might not appear immediately to view in FTP

Here's my answer, checks if the modification time is greater then something as well.

    <?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; 
?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top