简体   繁体   English

PHP将特定目录排除在Zip中

[英]PHP Exclude particular directories from being included in Zip

I have a particular directory called 'customers' that contains various directories. 我有一个名为“客户”的特定目录,其中包含各种目录。 One of the directories is called uploads. 目录之一称为上载。 Basically where users update their content. 基本上是用户更新其内容的地方。 This folder contains hundreds of pictures. 此文件夹包含数百张图片。

What I want is that all the files in this particular directory is added to a zip file except the 'uploads' directory which is backed up separately. 我想要的是将这个特定目录中的所有文件都添加到一个zip文件中,除了单独备份的“ uploads”目录。

This is the code I am trying to use: 这是我尝试使用的代码:

$zipFolderExclude = 'customers/uploads/*.*';

// create object
$zip = new ZipArchive();
// open archive
if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
    die ("Could not open archive");
}
// initialize an iterator
// pass it the directory to be processed
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($zipFolder));
// iterate over the directory
// add each file found to the archive
foreach ($iterator as $key=>$value) {
    if(!is_dir($zipFolderExclude) ) {
        $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
        echo $key . '<br/>';
    }
}
// close and save archive
$zip->close();
echo "Archive created successfully.";

The above code is zipping all files. 上面的代码压缩了所有文件。 I'm not sure what to do in this particular code: 我不确定在此特定代码中该怎么做:

if(!is_dir($zipFolderExclude) ) {
            $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
            echo $key . '<br/>';
        }

How can I exclude the uploads directory from the zip, please? 请问如何从zip中排除上载目录?

Since ZipArchive does not explicitly support exclusions (of some directories), you may apply two strategies: 由于ZipArchive不明确支持(某些目录中的)排除,因此您可以应用两种策略:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM