PHP将多级目录打包成zip文件
$zip = new ZipArchive(); //参数1:zip保存路径,参数2:ZIPARCHIVE::CREATE没有即是创建 if(!$zip->open("$exportPath.zip",ZIPARCHIVE::CREATE)) { echo "创建[$exportPath.zip]失败<br/>";return; } //echo "创建[$exportPath.zip]成功<br/>"; $this->createZip(opendir($exportPath),$zip,$exportPath); $zip->close();
/*压缩多级目录 $openFile:目录句柄 $zipObj:Zip对象 $sourceAbso:源文件夹路径 */ function createZip($openFile,$zipObj,$sourceAbso,$newRelat = '') { while(($file = readdir($openFile)) != false) { if($file=="." || $file=="..") continue; /*源目录路径(绝对路径)*/ $sourceTemp = $sourceAbso.'/'.$file; /*目标目录路径(相对路径)*/ $newTemp = $newRelat==''?$file:$newRelat.'/'.$file; if(is_dir($sourceTemp)) { //echo '创建'.$newTemp.'文件夹<br/>'; $zipObj->addEmptyDir($newTemp);/*这里注意:php只需传递一个文件夹名称路径即可*/ $this->createZip(opendir($sourceTemp),$zipObj,$sourceTemp,$newTemp); } if(is_file($sourceTemp)) { //echo '创建'.$newTemp.'文件<br/>'; $zipObj->addFile($sourceTemp,$newTemp); } } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战