Save compressed file to another directory

5

I have this function that does .zip of the current folder to save. But I want it to be saved in the backup folder.

$data = date("d_m_y");
$exten = "_backup.zip"; // Nome final com extensão

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($data.$exten, ZIPARCHIVE::CREATE) === true){
    $zip->Compact(".");
}
$zip->close();'
    
asked by anonymous 21.11.2014 / 14:21

1 answer

2

The directory where you want to save must be passed as a parameter when opening the file:

Example: ./backup/arquivo.zip

$diretorio = "./backup/";
$zip->open($diretorio.$data.$exten, ZIPARCHIVE::CREATE)

Remembering that we can use the getcwd () function to get the current directory:

$diretorio = getcwd() . "/backup/";
$zip->open($diretorio.$data.$exten, ZIPARCHIVE::CREATE)
    
21.11.2014 / 14:31