2012-09-06 161 views
2

我有一個拉鍊的功能,我想文件夾裏面的每個文件來壓縮zip文件 - 排除的文件夾

所以,路徑爲:

的public_html /表格/文件夾/ file_to_zip.xml

正如你可以在代碼中看到的那樣,$ files_to_zip數組有一個「FOLDER/file_to_zip.xml」路徑,每當它壓縮時,也會壓縮文件夾,我只希望文件被壓縮。

我能在這裏做什麼?

function create_zip($files = array(),$destination = '',$overwrite = true) { 

    $valid_files = array(); 
    if(is_array($files)) { 
     foreach($files as $file) { 
      if(file_exists($file)) { 
       $valid_files[] = $file; 
      } 
     } 
    } 
    if(count($valid_files)) { 
     $zip = new ZipArchive(); 
     if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { 
      return false; 
     } 
     foreach($valid_files as $file) { 
      $zip->addFile($file,$file); 
     } 

     $zip->close(); 

     return file_exists($destination); 
    } 
    else 
    { 
     return false; 
    } 
} 

$loja=$_GET['loja']; 

$connect = new MySQL('localhost','iloja_phc','sexparade'); 
$connect->Database('iloja_phc'); 
$posts = $connect->Fetch('ipad'); 

if ($posts && mysql_num_rows($posts) > 0) { 
    echo "Nome - ID - Email:<BR>"; 
    while ($record = mysql_fetch_array($posts)) { 
    $files_to_zip = array($loja.'/CL'.$record['rand'].'.xml'); 
    $result = create_zip($files_to_zip,'CL'.$record['rand'].'.zip'); 
     echo "<a href='/formulario/CL".$record['rand'].".zip'>".$record['nome']."</a> - ".$record['id']." - EMAIL: ".$record['email']."</br>"; 
    } 
} else { 
    echo "Erro! Algo correu mal..."; 
} 

回答

4

當你調用

$zip->addFile($file,$file); 

的第一個參數是文件路徑,第二個是「的localName」。如果從第二個參數中刪除文件夾,您將獲得所需的內容。

例子:

$zip->addFile("foo/bar.txt","bar.txt); 

爲您的特定情況下,我認爲這應該工作:

$zip->addFile($file, basename($file)); 

來源:PHP手冊評論http://php.net/manual/en/ziparchive.addfile.php

+0

你真棒:) 感謝的人。 – skills