2012-08-27 60 views
0

Iam因PHP 5.2x和“ZipArchive”而变得疯狂,以下代码不会引发错误,每个部分都会返回true,但不会创建ZIP文件。来自文件夹的PHP Zip文件 - 无错误 - 无存档

有人能看到iam没有看到的错误吗?

if ($handle = opendir($directory)) { 
     $zip = new ZipArchive(); 

     $filename = time() . '.zip'; 
     $path = APPLICATION_PATH . '/../download/'; 

     $status = $zip->open($path . $filename, ZIPARCHIVE::CREATE); 

     if ($status !== true) { 
      $this->log->err('Cant create zipArchive!'); 
      return false; 
     } 

     while (false !== ($file = readdir($handle))) { 
      if (!is_dir($file)) { 
       if (is_readable($file)) { 
        if (!$zip->addFile($file)) { 
         $this->log->err('Cant add File to archive: ' . $file); 
        } 
       } else { 
        $this->log->debug('Added file to archive: ' . $file); 
       } 
      } 
     } 

     closedir($handle); 

     if (!$zip->close()) { 
      $error = $zip->getStatusString(); 
      $this->log->err($error); 
      $this->log->err('Cant create archive..'); 
     } 
    } 
+0

尝试输出''真实路径来检查,你在这里做不成问题($路径$文件名)。 。你也可以尝试输出'is_writable(realpath($ path))'来检查权限。 – Florent

回答

1

你永远不会在你的ZIP存档中添加任何文件。这是你没有错误的唯一原因。

is_readable($file)总是返回false,并且当它打印出Added file to archive ...时,可能是因为你错位了你的else块。

您必须将文件夹添加到$file,你的情况应该是$directory

while (false !== ($file = readdir($handle))) { 
    if (!is_dir($directory . $file)) { 
     if (is_readable($directory . $file)) { 
      if (!$zip->addFile($directory . $file)) { 
       $this->log->err('Cant add File to archive: ' . $file); 
      } else { 
       $this->log->debug('Added file to archive: ' . $file); 
      } 
     } else { 
      $this->log->debug('File not readable: ' . $file); 
     } 
    } 
} 
+0

该死的:-)有时候我应该在一段时间后停止编码谢谢! – opHASnoNAME

+1

@ArneRie起初并不明显,但打印'$ zip-> numFiles'时我懂了:) – Tchoupi