2012-09-06 89 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