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

Php zipArchive如何在文件夹中保存文件夹,php,Php,我正在编写一个脚本,需要将文件夹和文件归档到其中,但如果一个文件夹中有另一个文件夹,我就不知道该怎么做。我将举例说明,这样规范就起作用了 警告:ZipArchive::close():读取错误:是第102行/ППааааааСаПаа/public_html/crm/drive/drive.php中的目录 // this function only for adding files! public function addFile ($filename, $localname = null,

我正在编写一个脚本,需要将文件夹和文件归档到其中,但如果一个文件夹中有另一个文件夹,我就不知道该怎么做。我将举例说明,这样规范就起作用了


警告:ZipArchive::close():读取错误:是第102行/ППааааааСаПаа/public_html/crm/drive/drive.php中的目录

// 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
正确的方法是使用on-查看文档中的选项

例如:

 // 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
        }
    }

}

我很抱歉,但这可能仍然是一个问题,我有必要在strinitsu文件夹中插入代码,并且没有错误