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);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM