2013-02-21 72 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("*");