Save compressed file to another directory

Asked

Viewed 421 times

5

I have this function that makes the .zip of the current folder to save. But I want you to save to the folder backup.

$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();`

1 answer

2


The directory you want to save to should be passed as parameter when opening the file:

Example: ./backup/arquivo.zip

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

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

$diretorio = getcwd() . "/backup/";
$zip->open($diretorio.$data.$exten, ZIPARCHIVE::CREATE)
  • It worked, but it was necessary to leave the directory path without the first bar: $directory = "backup/"

  • I corrected the answer, put the function getcwd(); which takes the directory you are currently in and puts the folder backup. Whenever an answer is correct, mark it as correct so that others with the same doubt know that the proposed solution worked. Another thing, to get it right use ./backup/arquivo.zip use the . at first.

Browser other questions tagged

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