2013-02-21 77 views
0

使用下列代碼創建並下載文件夾根目錄下所有文件的zip文件。 Ive得到了這條線將php設置爲*(全部)

$files_to_zip = array(
     'room1.jpg', 'room2.jpg' 
    ); 

,它可以讓你選擇哪些文件放入zip文件 - 但我的文件是cron作業的結果,所以那裏有一個新的每一天產生。我如何寫一個變量來說除了「」之外的所有文件,然後列出我不想在zipfolder ie中的文件。 index.php文件等..

到目前爲止,香港專業教育學院試着寫$files_to_zip = *;(顯然這不會排除任何文件),但只是拋出了一個解析錯誤:語法錯誤

這是完整的代碼波紋管:

<?php 
    /* creates a compressed zip file */ 
    function create_zip($files = array(),$destination = '',$overwrite = true) { 
     //if the zip file already exists and overwrite is false, return false 
     if(file_exists($destination) && !$overwrite) { return false; } 
     //vars 
     $valid_files = array(); 
     //if files were passed in... 
     if(is_array($files)) { 
     //cycle through each file 
     foreach($files as $file) { 
      //make sure the file exists 
      if(file_exists($file)) { 
      $valid_files[] = $file; 
      } 
     } 
     } 
     //if we have good files... 
     if(count($valid_files)) { 
     //create the archive 
     $zip = new ZipArchive(); 
     if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { 
      return false; 
     } 
     //add the files 
     foreach($valid_files as $file) { 
      $zip->addFile($file,$file); 
     } 
     //debug 
     //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; 

     //close the zip -- done! 
     $zip->close(); 

     //check to make sure the file exists 
     return file_exists($destination); 
     } 
     else 
     { 
     return false; 
     } 
    } 



    $files_to_zip = array(
     'room1.jpg', 'room2.jpg' 
    ); 
    //if true, good; if false, zip creation failed 
    $zip_name = 'my-archive.zip'; 
    $result = create_zip($files_to_zip,$zip_name); 

    if($result){ 
    header('Content-Type: application/zip'); 
    header('Content-disposition: attachment; filename=filename.zip'); 
    header('Content-Length: ' . filesize($zip_name)); 
    readfile($zip_name); 
    } 
    ?> 
+1

這不就是'glob()'函數嗎? – Barmar 2013-02-21 20:57:07

+0

@Barmar你應該發佈一個解釋'glob()'的答案。 ;) – Eric 2013-02-21 20:57:50

+0

是的謝謝@barmar工作很好..對不起,即時通訊仍然得到了PHP的嗡嗡聲..但感謝指出我在正確的方向 – sam 2013-02-21 20:59:38

回答

1

對文件系統執行通配符匹配的功能是​​3210。

$files_to_zip = glob("*");