/*php多文件压缩并且下载*/
function addFileToZip($path,$zip){
$handler=opendir($path); //打开当前文件夹由$path指定。
while(($filename=readdir($handler))!==false){
if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..’,不要对他们进行操作
if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归
addFileToZip($path."/".$filename, $zip);
}else{
//将文件加入zip对象;并减少目录层
$zip->addFile($path.$filename, $filename);
}
}
}
@closedir($path);
} //要下载的文件夹路径
$filePath = '..'.$_GET['activepath']. '/'. $_GET['filename'].'/';
//生成压缩文件名字
$zipFileName = '../download/'.$_GET['filename'] .'.zip'; $zip=new ZipArchive();
if($zip->open($zipFileName, ZipArchive::OVERWRITE)=== TRUE){
addFileToZip($filePath, $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
$zip->close(); //关闭处理的zip文件
} if(!file_exists($zipFileName))
{
echo '文件压缩失败!或者未生成压缩包!!';
exit;
} header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($zipFileName)); //文件名
header("Content-Type: application/zip"); //zip格式的
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: '. filesize($zipFileName)); //告诉浏览器,文件大小
@readfile($zipFileName);
  

  

04-02 17:17
查看更多