没有子文件夹的PHP ZIP存档

时间:2017-08-22 17:09:56

标签: php

我在堆栈覆盖流程上有这个类,我有一个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文件包含一个名为“foldername”的子文件夹?有一种方法可以将所有文件放在根zip中吗?

1 个答案:

答案 0 :(得分:0)

我将对您的代码留下一些评论,并尝试解释一下它的作用。

    <?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';}
    ?>

希望有所帮助