繁体   English   中英

没有子文件夹的 PHP ZIP 存档

[英]PHP ZIP Archive without subfolder

我在堆栈溢出上遇到了这个类,我有一个 php 问题:

<?php

$the_folder = 'path/foldername';
$zip_file_name = 'archived_name.zip';

class FlxZipArchive extends ZipArchive {
    /** Add a Dir with Files and Subdirs to the archive;;;;; @param string $location Real Location;;;;  @param string $name Name in Archive;;; @author Nicolas Heimann;;;; @access private  **/
public function addDir($location, $name) {
    $this->addEmptyDir($name);
     $this->addDirDo($location, $name);
 } // EO addDir;

    /**  Add Files & Dirs to archive;;;; @param string $location Real Location;  @param string $name Name in Archive;;;;;; @author Nicolas Heimann * @access private   **/
private function addDirDo($location, $name) {
    $name .= '/';         $location .= '/';
  // Read all Files in Dir
    $dir = opendir ($location);
    while ($file = readdir($dir))    {
        if ($file == '.' || $file == '..') continue;
      // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
        $do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
        $this->$do($location . $file, $name . $file);
    }
} 
}

$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
if($res === TRUE)    {
$za->addDir($the_folder, basename($the_folder)); $za->close();
}
else  { echo 'Could not create a zip archive';}
?>

这个类取一个文件夹,并制作一个 ZIP 文件。 但我对此有疑问。 为什么 zip 文件包含一个名为:“文件夹名”的子文件夹? 有没有办法将所有文件放在根 zip 中?

我会在你的代码上留下一些评论,并尝试解释一下它的作用。

    <?php

    $the_folder = 'path/foldername';//CHANGE THIS with your desired folder that contains the files
    $zip_file_name = 'archived_name.zip';// The archive name

    class FlxZipArchive extends ZipArchive {
        /** Add a Dir with Files and Subdirs to the archive;;;;; @param string $location Real Location;;;;  @param string $name Name in Archive;;; @author Nicolas Heimann;;;; @access private  **/
    public function addDir($location, $name) {
        $this->addEmptyDir($name);
         $this->addDirDo($location, $name);
     } // EO addDir;

        /**  Add Files & Dirs to archive;;;; @param string $location Real Location;  @param string $name Name in Archive;;;;;; @author Nicolas Heimann * @access public **/
    public function addDirDo($location, $name) {
        $name .= '/';         $location .= '/';
      // Read all Files in Dir
        $dir = opendir ($location);
        while ($file = readdir($dir))    {
            if ($file == '.' || $file == '..') continue;
          // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
            $do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
            $this->$do($location . $file, $name . $file);
        }
    } 
    }

    $za = new FlxZipArchive;
    $res = $za->open($zip_file_name, ZipArchive::CREATE);
    if($res === TRUE)    {
//CHANGE addDir method with addDirDo This will take all the files from the given directory and add them to the archive
    $za->addDirDo($the_folder, basename($the_folder)); $za->close();
    }
    else  { echo 'Could not create a zip archive';}
    ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM