Zipping directory with PHP

Asked

Viewed 3,788 times

8

I have a code that compacts file and directory. The problem is that it compresses the files and directories that are at the root, but the subdirectories they are not compressed, for example I have these directories:

folder/, folder/file.php, folder/other/paste2, archive1.php

it only compact folder/ and archive1.php ...

It should compress the php file, other/, pasture2/

Code:

<?php

function Show_files($local){

    $zip = new ZipArchive();

    if($zip->open('compact.zip', ZIPARCHIVE::CREATE) == TRUE){

    $open = opendir($local);

    while($folder = readdir($open)){

        if(is_dir($local.$folder) && $folder != '.' && $folder != '..'){

            echo $local.$folder.'<br>';

            $zip->addEmptyDir($folder);

            Show_files($local.$folder.'/');

        }elseif(is_file($local.$folder) && $folder != '.' && $folder != '..'){

            $zip->addFile($local.$folder, $folder);

                echo $local.$folder.'<br>';
        }
    }
}

}
    $raiz = str_replace("\\", "/", getcwd())."/";
    Show_files($raiz);

?>

1 answer

6


What’s going on is that you’re rewriting the file because it is a recursive function, then the instance $zip is created every time you pass the recursive function call. To get it right, create the instance $zip and pass as function parameter.

Example with function:

<?php
    function Compact($zip, $cwd) {
        $open = opendir($cwd);
        while($folder = readdir($open))
        {
            if ($folder != '.' && $folder != '..'){
                if (is_dir($cwd.'/'.$folder))
                {
                    $dir = str_replace('./', '',($cwd.'/'.$folder));
                    $zip->addEmptyDir($dir);
                    Compactar($zip, $dir);
                }
                elseif (is_file($cwd.'/'.$folder))
                {
                    $arq = str_replace('./', '',$cwd.'/'.$folder);                  
                    $zip->addFile($arq);                                        
                }
            }
        }
    }

    $zip = new ZipArchive();
    if ($zip->open("arquivoFAfa.zip", ZIPARCHIVE::CREATE) === true){
        Compact($zip, ".");
    }
    $zip->close();

Example with class extending ZipArchive

<?php
    class Zipper extends ZipArchive 
    {
        public function Compact($cwd) {
            $open = opendir($cwd);
            while($folder = readdir($open))
            {
                if ($folder != '.' && $folder != '..')
                {
                    if (is_dir($cwd.'/'.$folder))
                    {
                        $dir = str_replace('./', '',($cwd.'/'.$folder));
                        $this->addEmptyDir($dir);
                        $this->Compact($dir);
                    } 
                    elseif (is_file($cwd.'/'.$folder))
                    {
                        $arq = str_replace('./', '',$cwd.'/'.$folder);                  
                        $this->addFile($arq);                   
                    }
                }
            }
        }
    }

    $zip = new Zipper();
    if ($zip->open("arquivoFAClassAbc.zip", ZIPARCHIVE::CREATE) === true){
        $zip->Compact(".");
    }
    $zip->close();

Tip: Use the second option, because you won’t have this instance problem, although the first one is only to create the out instance as reported at the beginning of the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.