2015-06-07 192 views
0

我在PHP中使用創建zip文件,但我想排除zip文件本身。PHP創建zip但排除zip文件

$rootPath = realpath('../../'); 

    $zip = new ZipArchive(); 
    $zip->open('../../includes/updatenew.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE); 

    $files = new RecursiveIteratorIterator(
     new RecursiveDirectoryIterator($rootPath), 
     RecursiveIteratorIterator::LEAVES_ONLY 
    ); 

    foreach ($files as $name => $file) 
    { 
     if (!$file->isDir()) 
     { 
      $filePath = $file->getRealPath(); 
      $relativePath = substr($filePath, strlen($rootPath) + 1); 

      $zip->addFile($filePath, $relativePath); 
     } 
    } 
    $zip->close(); 

此代碼首先創建了../../includes/updatenew.zip然後拉鍊整個目錄,但我怎麼能排除在zip添加的updatenew.zip?

回答

0

更新從

if (!$file->isDir()) 

您的條件類似的東西

if (!$file->isDir() AND (strpos($name, 'updatenew.zip') === FALSE)) 

這會跳過含有「updatenew.zip」在文件名中的所有文件。