2013-03-14 136 views
0

我正在尋找解壓縮文件夾內容的解決方案,其中包括文件夾中的所有子文件夾,但不包括主文件夾本身壓縮文件夾的內容php,而不是整個文件夾

我從這個功能,增加了整個文件夾到一個zip壓縮文件

function addFolderToZip($dir, $zipArchive){ 
    if (is_dir($dir)) { 
     if ($dh = opendir($dir)) { 

      //Add the directory 
      $zipArchive->addEmptyDir($dir); 

      // Loop through all the files 
      while (($file = readdir($dh)) !== false) { 

       //If it's a folder, run the function again! 
       if(!is_file($dir . $file)){ 
        // Skip parent and root directories 
        if(($file !== ".") && ($file !== "..")){ 
         addFolderToZip($dir . $file . "/", $zipArchive); 
        } 

       }else{ 
        // Add the files 
        $zipArchive->addFile($dir . $file); 

       } 
      } 
     } 
    } 
} 

開始,但我不明白如何將功能更改爲(可能使用第三個參數?)

$z = new ZipArchive(); 
$z->open("zips/new.zip", ZIPARCHIVE::CREATE); 
addFolderToZip("unzipped/",$z); 
$z->close(); 

回答

1

我知道了,我已經創建了一個參數$局部路徑,設置爲「」:

function add_folder_in_path($folder_path,$local_path,$z) 
{ 
    $dh=opendir($folder_path); 
    while (($file = readdir($dh)) !== false) 
    { 
     if(($file !== ".") && ($file !== "..")) 
     { 
     if (is_file($folder_path.$file)) 
      { 

        $z->addFile($folder_path.$file,$local_path.$file); 
      } 
     else 
      { 
       add_folder_in_path($folder_path.$file."/",$local_path.$file."/",$z); 
      } 
     } 


    } 
} 

這裏是我叫我t

$z = new ZipArchive(); 
$z->open("zipped/new.zip", ZIPARCHIVE::CREATE); 
echo "<br>"; 
add_folder_in_path("myzips/","",$z); 
$z->close();  
相關問題