2012-08-27 69 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