2

我正在尝试以与我的计算机中相同的目录格式制作文件数组的 zip 我编写的脚本正在运行,唯一的问题是它覆盖了目录的第一个元素,而第二个元素只有最后一个元素在目录数组中的 zip 文件中,我需要从提供的目录数组中添加所有文件。这是我的代码,请告诉我我做错了什么。

// Get real path for our folder
$rootPaths = array(
               'D:\xampp\htdocs\moko\wp-content\plugins\moko\classes',
               'D:\xampp\htdocs\moko\wp-content\plugins\moko\templates',
             );

$valid_files = array();
if (is_array($rootPaths)) {
     foreach ($rootPaths as $new_files) {
          if (file_exists($new_files)) {
               $valid_files[] = $new_files;
          }
     }
}

// Initialize archive object
if (count($valid_files)) {
    $zip = new ZipArchive();
    $zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Create recursive directory iterator
/** @var SplFileInfo[] $files */
foreach ($valid_files as $rootPath) {
    $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY
    );
}

foreach ($files as $name => $file) {
    // Skip directories (they would be added automatically)
    if (!$file->isDir()) {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);

        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
    }
}
// Zip archive will be created only after closing object
$zip->close();
}

此脚本仅使用以下目录的文件和目录结构创建 zip 文件。

'D:\xampp\htdocs\moko\wp-content\plugins\moko\templates'

4

1 回答 1

0

您根本不需要创建变量$valid_files。您正在谈论的循环是检查目录中的有效文件,仅检查$rootPaths变量中的目录是否存在。此外,我还更改了您将文件添加到 zip 的方式。$files循环每次执行时都会覆盖变量。看看下面的代码。

// Get real path for our folder
$rootPaths = array(
    'D:\xampp\htdocs\moko\wp-content\plugins\moko\classes',
    'D:\xampp\htdocs\moko\wp-content\plugins\moko\templates',
);

if (is_array($rootPaths)) {
    foreach ($rootPaths as $key => $new_files) {
        if (!file_exists($new_files)) {
            unset($rootPaths[$key]);
        }
    }
}
// Initialize archive object
if (count($rootPaths)) {
    $zip = new ZipArchive();
    $zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Create recursive directory iterator
    /** @var SplFileInfo[] $files */
    foreach ($rootPaths as $rootPath) {
        $files = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY
        );
        foreach ($files as $name => $file) {
            // Skip directories (they would be added automatically)
            if (!$file->isDir()) {
                // Get real and relative path for current file
                $filePath = $file->getRealPath();
                $relativePath = substr($filePath, strlen($rootPath) + 1);

                // Add current file to archive
                $zip->addFile($filePath, $relativePath);
            }
        }
    }
// Zip archive will be created only after closing object
    $zip->close();
}
于 2018-07-06T08:50:58.887 回答