zipArchive 如何在文件夹中保存文件夹

瓦迪姆

我正在编写一个脚本,需要在其中存档文件夹和文件,但是如果一个文件夹中有另一个文件夹,我无法弄清楚该怎么做。我将通过示例进行解释,以便规范起作用

Warning: ZipArchive::close(): Read error: Is a directory in /путь до скрипта/public_html/crm/drive/drive.php on line 102

Folder +
     File1
     File2
     And so on (so it works)

But does not want to work like that

Folder +
       File1
       FOLDER (this does not work)

问题是如何做到这一点,以便脚本看到它也下载的文件夹,并且我看到该文件夹​​中的文件夹也分别下载了文件夹中的文件?这是我的脚本

if (isset($_POST['createPath'])) {//Check that the button is clicked
     
$zip = new ZipArchive(); // Create an archive
$zip_name = time().".zip"; // file name
if ($zip->open($zip_name, ZipArchive::CREATE) !== true) { // open file
  die ("Could not open archive");//If the file does not open
}
$var = $_POST["pathUpload"];// Array of variables that are passed through the form
foreach ($var as $key_var) {//  We process the array in a loop
$iterator = new RecursiveDirectoryIterator($key_var);//There is a recursive search of the file system directories
foreach ($iterator as $key => $value) {// We process an array of files
  $path = pathinfo($value);//Check the path or revert the path to the file
  if ($path['basename'] == '.' || $path['basename'] == '..') continue;//Check those files that you download if there are points in the files then download

  $zip->addFile(realpath($key), $key);//Add the file to the server


 
}

$zip->close();//Close archive
    if (file_exists($zip_name)) {
        // Give the file to download
        header('Content-type: application/zip', 'charset=utf-8');
        header('Content-Disposition: attachment; filename="'.$zip_name.'"');
         ob_end_flush();//Buffering since without it nothing will work
        readfile($zip_name); //Read the file

        unlink($zip_name);//Delete the variable
    }
 }

}  

米贝德

错误是因为您尝试使用方法将目录添加到 Zip

// this function only for adding files!
public function addFile ($filename, $localname = null, $start = 0, $length = 0) {}

该方法允许您添加目录

public function addEmptyDir ($dirname) {}

您遇到的另一个问题是您以错误的方式使用目录迭代器。

// this way only loop on directories in 
$iterator = new RecursiveDirectoryIterator($key_var);//There is a recursive search of the file system directories

正确的方法是使用RecursiveIteratorIteratorRecursiveDirectoryIterator -看看在文档的选项。

例子:

 // the right way to recursive get list of the file system directories and files
$iterator = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($key_var, RecursiveDirectoryIterator::SKIP_DOTS), // skip . and ..
            RecursiveIteratorIterator::SELF_FIRST,
            RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);

所以为了让它工作,你的代码应该是这样的:

<?php

if (isset($_POST['createPath'])) {//Check that the button is clicked

    $zip      = new ZipArchive(); // Create an archive
    $zip_name = time() . ".zip"; // file name
    if ($zip->open($zip_name, ZipArchive::CREATE) !== true) { // open file
        die ("Could not open archive");//If the file does not open
    }

    $var = $_POST["pathUpload"];// Array of variables that are passed through the form
    foreach ($var as $key_var) {//  We process the array in a loop

        // There is a recursive search of the file system directories
        $iterator = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($key_var, RecursiveDirectoryIterator::SKIP_DOTS), // skip . and ..
            RecursiveIteratorIterator::SELF_FIRST,
            RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
        );

        // all directories and subdir
        foreach ($iterator as $path => $dir) {
            if (!$dir->isDir()) && is_file($path) { 
                $zip->addFile(realpath($path)); //Add the file to the server
            }
            if ($dir->isDir()) {
                // do nothing
            }
        }

        $zip->close(); //Close archive
        if (file_exists($zip_name)) {
            // Give the file to download
            header('Content-type: application/zip', 'charset=utf-8');
            header('Content-Disposition: attachment; filename="' . $zip_name . '"');
            ob_end_flush();//Buffering since without it nothing will work
            readfile($zip_name); //Read the file

            unlink($zip_name);//Delete the variable
        }
    }

}

快乐编码:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ZipArchive返回空文件夹C#

来自分类Dev

php ZipArchive-仅添加文件而不添加父文件夹

来自分类Dev

使ZipArchive :: close()起作用并要创建php文件夹的问题

来自分类Dev

ZipArchive提取-单个文件

来自分类Dev

ZipArchive无效的Zip文件

来自分类Dev

Ziparchive:如何从 ziparchive 关闭创建的条目

来自分类Dev

在文件夹中保存X次

来自分类Dev

如何在Windows应用程序的文件夹中保存文件

来自分类Dev

tinyfiledialogs如何在俄语命名文件夹中保存文件

来自分类Dev

如何在Windows应用程序的文件夹中保存文件

来自分类Dev

如何在bash脚本中保留文件夹结构?

来自分类Dev

如何在Android上的值文件夹中保存一个double

来自分类Dev

如何在互联网浏览器的多个文件夹中保存书签?

来自分类Dev

如何在OneDrive中找到“保存的图片”文件夹?

来自分类Dev

如何在Gimp中设置默认保存文件夹

来自分类Dev

如何使用C#创建文件夹并在其中保存文件

来自分类Dev

ZipArchive返回空文件C#

来自分类Dev

ZipArchive ::检查文件扩展名

来自分类Dev

如何在卸载过程中保留扩展名的文件夹/文件

来自分类Dev

使用数据工厂复制文件时如何在 blob 存储中保留文件夹结构

来自分类Dev

如何通过文件夹名将文件保存到文件夹中?

来自分类Dev

如何使用 EmguCV c# 在 Content 文件夹中保存 IP 摄像机快照

来自分类Dev

如何在用户选择的根文件夹中创建包含某些子文件夹的文件夹?

来自分类Dev

如何在文件夹和子文件夹内尾随所有日志文件?

来自分类Dev

如何在ckfinder中显示所有文件夹,子文件夹和文件

来自分类Dev

如何在Windows 7中仅搜索文件夹而不是文件夹和文件

来自分类Dev

如何在Linux中的mv文件夹中保留其mtime?

来自分类Dev

如何在SUSE中保留一个已经存在的主文件夹

来自分类Dev

从变量传递时,如何在名称中保留空格的转义文件夹?

Related 相关文章

  1. 1

    ZipArchive返回空文件夹C#

  2. 2

    php ZipArchive-仅添加文件而不添加父文件夹

  3. 3

    使ZipArchive :: close()起作用并要创建php文件夹的问题

  4. 4

    ZipArchive提取-单个文件

  5. 5

    ZipArchive无效的Zip文件

  6. 6

    Ziparchive:如何从 ziparchive 关闭创建的条目

  7. 7

    在文件夹中保存X次

  8. 8

    如何在Windows应用程序的文件夹中保存文件

  9. 9

    tinyfiledialogs如何在俄语命名文件夹中保存文件

  10. 10

    如何在Windows应用程序的文件夹中保存文件

  11. 11

    如何在bash脚本中保留文件夹结构?

  12. 12

    如何在Android上的值文件夹中保存一个double

  13. 13

    如何在互联网浏览器的多个文件夹中保存书签?

  14. 14

    如何在OneDrive中找到“保存的图片”文件夹?

  15. 15

    如何在Gimp中设置默认保存文件夹

  16. 16

    如何使用C#创建文件夹并在其中保存文件

  17. 17

    ZipArchive返回空文件C#

  18. 18

    ZipArchive ::检查文件扩展名

  19. 19

    如何在卸载过程中保留扩展名的文件夹/文件

  20. 20

    使用数据工厂复制文件时如何在 blob 存储中保留文件夹结构

  21. 21

    如何通过文件夹名将文件保存到文件夹中?

  22. 22

    如何使用 EmguCV c# 在 Content 文件夹中保存 IP 摄像机快照

  23. 23

    如何在用户选择的根文件夹中创建包含某些子文件夹的文件夹?

  24. 24

    如何在文件夹和子文件夹内尾随所有日志文件?

  25. 25

    如何在ckfinder中显示所有文件夹,子文件夹和文件

  26. 26

    如何在Windows 7中仅搜索文件夹而不是文件夹和文件

  27. 27

    如何在Linux中的mv文件夹中保留其mtime?

  28. 28

    如何在SUSE中保留一个已经存在的主文件夹

  29. 29

    从变量传递时,如何在名称中保留空格的转义文件夹?

热门标签

归档