首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

[分享]php ZipArchive实现文件夹打包下载

发布于 2025-04-11 21:35:03
0
28
function addFileToZip($strPath, ZipArchive $pZip){
  $filelist=scandir($strPath);
  if(empty($filelist))die("空目录");
  foreach ($filelist as $k => $filename) {
    if ($filename != "." && $filename != ".."){
      $pZip->addFile($strPath.$filename);
    }
  }
}
$tmpdir="uploads";
$strPath = '打包名称.zip';
try{
  $pZip = new ZipArchive();
  $pZip->open($strPath, ZipArchive::CREATE);
  addFileToZip($tmpdir, $pZip);
  $pZip->close();
}
catch (Exception $e){
  throw new BusinessException('文件打包异常:'.$e->getMessage());
}
header("location:{$strPath}");die;
评论
一个月内的热帖推荐
站长交流