PHP一键打包压缩目录文件代码示例

 2870

PHP一键打包压缩目录文件代码示例,有时候需要对一个文件目录打包整理,网站找到这么一段代码,分享给大家看看:

  1. <?php     
  2. $button=$_POST['button'];     
  3. if($button=="开始打包")     
  4. {     
  5.     $zip = new ZipArchive();     
  6.     $filename = "./".date("Y-m-d")."_".md5(time())."_zy.zip";     
  7.     if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {     
  8.         exit("无法创建 <$filename>\n");     
  9.         }     
  10.     $files = listdir();     
  11.     foreach($files as $path)     
  12.     {     
  13.         $zip->addFile($path,str_replace("./","",str_replace("\\","/",$path)));    
  14.     }    
  15.     echo "压缩完成,共压缩了: " . $zip->numFiles . "个文件\n";    
  16.     $zip->close();    
  17. }    
  18. Function listdir($start_dir='.') {    
  19.   $files = array();    
  20.   if (is_dir($start_dir)) {    
  21.    $fh = opendir($start_dir);    
  22.    while (($file = readdir($fh)) !== false) {    
  23.      if (strcmp($file, '.')==0 || strcmp($file, '..')==0) continue;    
  24.      $filepath = $start_dir . '/' . $file;    
  25.      if ( is_dir($filepath) )    
  26.        $files = array_merge($files, listdir($filepath));    
  27.      else   
  28.        array_push($files, $filepath);    
  29.    }    
  30.    closedir($fh);    
  31.   } else {    
  32.    $files = false;    
  33.   }    
  34.  return $files;    
  35. }    
  36. ?>    
  37. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >     
  38. <html>     
  39.     <head>     
  40.         <title>打包工具</title>     
  41.         <meta http-equiv="Content-Type" content="text/html; charset=gb2312">    
  42.     </head>    
  43.     <body>    
  44.         <form name="form1" method="post" action="">    
  45.             <hr size="1">    
  46.             
  47.             <P> <input type="submit" name="button" value="开始打包" /></P>     
  48.                   
  49.         </form>     
  50.     </body>     
  51. </html>


本文网址:https://www.zztuku.com/index.php/detail-11929.html
站长图库 - PHP一键打包压缩目录文件代码示例
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐