压缩文件需要包含空文件夹

时间:2019-11-22 13:19:21

标签: php zip ziparchive

我目前具有以下目录结构:

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

等。我正在使用下面显示的代码通过将其压缩来备份“ upload-here”文件夹。

<?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

等等等

2 个答案:

答案 0 :(得分:2)

请尝试一下。您可以获得包含空文件夹的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);
}}

答案 1 :(得分:0)

您可以检查文件夹是否为空,然后使用[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