Zip 文件需要包含空文件夹

Zip files need to include empty folders

我目前的目录结构是这样的:

upload-here
upload-here/258/Image1.jpg
upload-here/259/Image2.jpg
upload-here/260/NO IMAGE
upload-here/261/somepicturename.jpg

等等等等。我正在使用下面显示的代码通过压缩来备份上传文件夹。

<?php
// Get real path for our folder
$rootPath = realpath('/full-path-to/upload-here/');

// Initialize archive object
$zip = new ZipArchive();
$zip->open('upload-here.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$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();
?>

这只适用于一个问题:如果文件夹中没有图像,则不会备份该文件夹。

我如何调整代码以使其也压缩空文件夹(以便确切的结构与在线内容匹配)?

=================== 添加了

echo "<Br>". $filePath . "<br>".  $relativePath . "<br>".  $name ;

前两个}。这给出:

/home/fullpath/public_html/upload-here

/home/fullpath/public_html/upload-here/.
/home/fullpath/public_html

/home/fullpath/public_html/upload-here/..
/home/fullpath/public_html/upload-here/73
73
/home/fullpath/public_html/upload-here/73/.
/home/fullpath/public_html/upload-here

/home/fullpath/public_html/upload-here/73/..
/home/fullpath/public_html/upload-here/283
283
/home/fullpath/public_html/upload-here/283/.
/home/fullpath/public_html/upload-here

/home/fullpath/public_html/upload-here/283/..
/home/fullpath/public_html/upload-here/284
284
/home/fullpath/public_html/upload-here/284/.
/home/fullpath/public_html/upload-here

/home/fullpath/public_html/upload-here/284/..
/home/fullpath/public_html/upload-here/291
291
/home/fullpath/public_html/upload-here/291/.
/home/fullpath/public_html/upload-here

/home/fullpath/public_html/upload-here/291/..
/home/fullpath/public_html/upload-here/292
292
/home/fullpath/public_html/upload-here/292/.
/home/fullpath/public_html/upload-here

等等等等

您可以检查文件夹是否为空,然后使用 [addEmptyDir()][1] 将其添加到存档中:

    foreach ($files as $name => $file)
    {
     $filePath = $file->getRealPath();
     $relativePath = substr($filePath, strlen($rootPath) + 1);
    // Skip directories (they would be added automatically)
    if (!$file->isDir())
    {
        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
    } else {
        if(count(glob($name."/*")) == 0) {
           $zip->addEmptyDir(basename($name));
    }
  }
}

  [1]: https://www.php.net/manual/en/ziparchive.addemptydir.php

请试一试。您可以获得包含空文件夹的 zip 文件。

foreach ($files as $name => $file) {
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Skip directories (they would be added automatically)
if (!$file->isDir()) {
    // Add current file to archive
    $zip->addFile($filePath, $relativePath);
} else {
    if($relativePath !== false)
        $zip->addEmptyDir($relativePath);
}}