F__M
F__M

Reputation: 1598

PHP - Zip a directory but exclude one

I'm trying to create a zip from a directory.

But how can I exclude one directory in to this directory ?

For example:

www/          <-- Main directory I would like to zip
www/abc/      <-- Should be zip
www/def/      <-- Should be zip
www/_bk/     <-- Should't be zip

Here my actual code:

$toBackup = 'www/';
$backupDestination = '';

if(file_exists($backupDestination.'datas.tar.gz')) {
    unlink($backupDestination.'datas.tar.gz');
}

chdir($toBackup);

$fileName = "datas.tar";

$command = "tar cvf {$fileName} *";
$lastLine = exec($command, $output);

$command = "gzip {$fileName}";
$lastLine = exec($command, $output);

$command = "mv {$fileName}.gz {$backupDestination}";
$lastLine = exec($command, $output);

Upvotes: 0

Views: 62

Answers (1)

Alex Barker
Alex Barker

Reputation: 4400

PHP has a zip extension that is probably better suited for your needs. See: https://www.php.net/manual/en/book.zip.php

If you must use the command line, tar can take multiple --exclude flags. See https://linux.die.net/man/1/tar

Upvotes: 1

Related Questions