ZIP:包含父目录(PHP)

ZIP:包含父目录(PHP),php,zip,Php,Zip,我需要一些帮助,我有压缩文件夹的代码,我的问题是它没有在压缩文件中包含目标文件夹或父文件夹 Test -- 1 -- 2 zip文件中只有1和2,我想包括测试目录 这是我的密码 $source='/testing'; $destination='/test'; $img_dir = array("1", "2"); $arrlength = count($img_dir); foreach ($img_dir as $k => $v) { zipD

我需要一些帮助,我有压缩文件夹的代码,我的问题是它没有在压缩文件中包含目标文件夹或父文件夹

  Test
    -- 1
    -- 2
zip文件中只有1和2,我想包括测试目录

这是我的密码

$source='/testing';
$destination='/test';

$img_dir = array("1", "2");
$arrlength = count($img_dir);


   foreach ($img_dir as $k => $v) {
    zipData($destination.'/'.$v, $destination.'/img_'.$v.'.zip');
}

function zipData($source, $destination) {
    if (extension_loaded('zip')) {
        if (file_exists($source)) {
            $zip = new ZipArchive();
            if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
                $source = realpath($source);
                if (is_dir($source)) {
                    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
                    foreach ($files as $file) {
                        $file = realpath($file);
                        if (is_dir($file)) {
                            $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
                        } else if (is_file($file)) {
                            $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
                        }
                    }
                } else if (is_file($source)) {
                    $zip->addFromString(basename($source), file_get_contents($source));
                }
            }
            return $zip->close();
        }
    }
    return false;
}

将整个目录树添加到存档的最佳方法是使用

您在PHP手册中有一个很好的示例,它实际上正是您自己想要编写的:。你应该先检查手册

在第一条用户评论中:

Zip a folder (include itself). 
Usage: 
  HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip'); 

<?php 
class HZip 
{ 
  /** 
   * Add files and sub-directories in a folder to zip file. 
   * @param string $folder 
   * @param ZipArchive $zipFile 
   * @param int $exclusiveLength Number of text to be exclusived from the file path. 
   */ 
  private static function folderToZip($folder, &$zipFile, $exclusiveLength) { 
    $handle = opendir($folder); 
    while (false !== $f = readdir($handle)) { 
      if ($f != '.' && $f != '..') { 
        $filePath = "$folder/$f"; 
        // Remove prefix from file path before add to zip. 
        $localPath = substr($filePath, $exclusiveLength); 
        if (is_file($filePath)) { 
          $zipFile->addFile($filePath, $localPath); 
        } elseif (is_dir($filePath)) { 
          // Add sub-directory. 
          $zipFile->addEmptyDir($localPath); 
          self::folderToZip($filePath, $zipFile, $exclusiveLength); 
        } 
      } 
    } 
    closedir($handle); 
  } 

  /** 
   * Zip a folder (include itself). 
   * Usage: 
   *   HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip'); 
   * 
   * @param string $sourcePath Path of directory to be zip. 
   * @param string $outZipPath Path of output zip file. 
   */ 
  public static function zipDir($sourcePath, $outZipPath) 
  { 
    $pathInfo = pathInfo($sourcePath); 
    $parentPath = $pathInfo['dirname']; 
    $dirName = $pathInfo['basename']; 

    $z = new ZipArchive(); 
    $z->open($outZipPath, ZIPARCHIVE::CREATE); 
    $z->addEmptyDir($dirName); 
    self::folderToZip($sourcePath, $z, strlen("$parentPath/")); 
    $z->close(); 
  } 
} 
?>
Zip文件夹(包括其本身)。
用法:
HZip::zipDir('/path/to/sourceDir','/path/to/out.zip');

好吧,就我所知,不要将父目录添加到存档中。你有条件
if(is_dir($source)){
并且在它里面你迭代了里面的文件并只添加了它们。所以我不太明白问题出在哪里。你已经在父目录里面添加了子目录(
$zip->addEmptyDir()
)如果您想添加一个目录和所有的子目录,请查看
::addGlob()
,这样做可以节省大量的时间processing@DawidFerenczy对不起,我是新来的,我看不懂。请逐行读代码:在
$zip->open()之后
,检查
$source
中的路径是否为目录(
if(is_dir($source))
),然后遍历该目录中的文件和子目录(
foreach($files as$file){
)然后将它们添加到归档文件中。因此,在开始添加文件和其中的子目录(打开后)之前,只需将根目录添加到归档文件中即可。