Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Zip文件需要包含空文件夹_Php_Zip_Ziparchive - Fatal编程技术网

Php Zip文件需要包含空文件夹

Php Zip文件需要包含空文件夹,php,zip,ziparchive,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 等等。我正在使用下面显示的代码通过压缩上传文件夹来备份上传文件夹 <?php // Get real path for our folder $rootPath = realpath('/full-path-to/

我目前有以下目录结构:

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();
?>

etc etc

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

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

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

似乎仍在跳过空文件夹(刚刚测试过,zip只包含包含文件的文件夹,没有空文件夹;您尝试过$relativePath和$filePath吗?并且$name变量只包含文件名还是完整路径?我还编辑了代码,在某种情况下遗漏了一个否定项。因此,请参阅EditsHanks以获取帮助,很遗憾,上述操作现在只会导致zip文件为空。)使用的y变量如下所示etc@user6796243我对代码做了一些更改,现在就试试。另外,我知道它们只是使用的变量,但我想知道它们的值。似乎仍然跳过了空文件夹(刚刚测试过,zip只包含包含文件的文件夹,没有空文件夹;您尝试过$relativePath和$filePath吗?并且$name变量只包含文件名还是完整路径?我还编辑了代码,在某种情况下遗漏了一个否定项。因此,请参阅EditsHanks以获取帮助,很遗憾,上述操作现在只会导致zip文件为空。)使用的y变量如下所示etc@user6796243我对代码做了一些更改,现在就试试。另外,我知道它们只是使用的变量,但我想知道它们的值。很酷,很高兴听到:)很酷,很高兴听到:)