【问题标题】:Zip files need to include empty foldersZip 文件需要包含空文件夹
【发布时间】:2019-11-22 22:49:15
【问题描述】:

我目前有这个目录结构:

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

等等等等

【问题讨论】:

    标签: php zip ziparchive


    【解决方案1】:

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

    【讨论】:

    • 酷,很高兴听到这个消息:)
    【解决方案2】:

    您可以检查文件夹是否为空,然后使用 [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 仅包含带有文件的文件夹,没有空文件夹;
    • 你试过 $relativePath 和 $filePath 了吗? $name 变量是只包含文件名还是完整路径?我还编辑了代码,我错过了一个条件中的否定。所以看看编辑
    • 感谢您的帮助,遗憾的是,上面的结果现在是一个空的 zip 文件
    • 唯一使用的变量是此处显示的变量等
    • @user6796243 我对代码做了一些修改,现在试试。另外,我知道它们只是使用的变量,但我想知道它们的值。
    猜你喜欢
    • 2013-09-14
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 2016-10-02
    • 2021-08-24
    相关资源
    最近更新 更多