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>
路是自己走出来的,而不是选出来的。