Zip文件需要包含空文件夹

问题描述 投票:0回答:2

我目前具有此目录结构:

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

etc等。我正在使用下面显示的代码,通过将其压缩来对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

etc etc

php zip ziparchive
2个回答
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

0
投票

请尝试。您可以获得包含空文件夹的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);
}}
© www.soinside.com 2019 - 2024. All rights reserved.