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

时间:2017-07-03 06:16:49

标签: php

我正在编写一个需要存档内部文件夹和文件的脚本,但如果在一个文件夹中有另一个文件夹,我无法弄清楚如何操作。我将通过实例解释,所以规范工作



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

}  




1 个答案:

答案 0 :(得分:0)

该错误是因为您尝试使用方法

将目录添加到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

正确的方法是在RecursiveIteratorIterator上使用RecursiveDirectoryIterator - 查看文档中的选项。

示例:

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

}

快乐的编码:)

相关问题