ThinkPHP框架下如何用PHP自带的ZipArchive类打包压缩文件

public function downloadzip(){
$catid = $this->request->param('catid/d', 0);
$id = $this->request->param('id/d', 0);
$category = getCategory($catid);
if (empty($category)) {
$this->error('该栏目不存在!');
}
$modelid = $category['modelid'];
$fieldList = $this->Cms_Model->getFieldList($modelid, $id);

$attatchs=$fieldList['uploadmaterial']['value'];

$attatchsarray=explode(',',$attatchs);
$zip=new \ZipArchive();
$downpath=[];
$timefils=date("Ymd",time());
if(!file_exists('uploads/zip/'.$timefils)){
mkdir ('uploads/zip/'.$timefils,0777,true);
}

$zipName ='uploads/zip/'.$timefils.'/download'.$id.'.zip';

if($zip->open($zipName,\ZipArchive::OVERWRITE | \ZipArchive::CREATE)==TRUE){
foreach($attatchsarray as $key=>$value){
$downpath=dirname(ROOT_PATH).get_file_path($value);
$downpathname=get_file_name($value); //改文件一定得为本地地址不可以为域名地址
$zip->addFile($downpath, basename($downpathname)); //basename($downpathname)这个是压缩包里面的文件名称

}
}
$zip->close();

if(!file_exists($zipName)){
$this->error('文件创建失败');
}
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipName);
header('Content-Length: ' . filesize($zipName));//文件大小
readfile($zipName);
}

转载于:https://www.cnblogs.com/luluzc/p/11396090.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值