php在线打包下载

来源:互联网 发布:sql往表中添加新数据 编辑:程序博客网 时间:2024/04/29 05:33
<?php
$button="";
if(isset($_POST['button'])){
    $button=$_POST['button'];
}


if($button=="下载")
{
    $zip = new ZipArchive();
    $filename = "./myimages.zip";
    if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
        exit("无法创建 <$filename>\n");
        }
    $files = listdir();
    foreach($files as $path)
    {
        $zip->addFile($path,str_replace("./","",str_replace("\\","/",$path)));
    }
    echo "压缩完成,共压缩了: " . $zip->numFiles . "个文件\n";
    $zip->close();
}
Function listdir($start_dir='./myimages/') {
  $files = array();
  if (is_dir($start_dir)) {
   $fh = opendir($start_dir);
   while (($file = readdir($fh)) !== false) {
     if (strcmp($file, '.')==0 || strcmp($file, '..')==0) continue;
     $filepath = $start_dir . '/' . $file;
     if ( is_dir($filepath) )
       $files = array_merge($files, listdir($filepath));
     else
       array_push($files, $filepath);
   }
   closedir($fh);
  } else {
   $files = false;
  }
 return $files;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
    <head>
        <title>下载</title>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
    <body>
        <form name="form1" method="post" action="">
            <hr size="1">
            <P> <input type="submit" name="button" value="下载" /></P>
        </form>
    </body>
</html>
0 0