使用 zipArchive 實現資源打包下載
$id = $request->get('id');
$content = MmsProject::where(['id'=>$id])->value('content');
if(!count($content)) exception("資訊不存在");
$count = 0;
$filename = public_path().'/file.zip';
$zip = new \ZipArchive();
if ($zip->open($filename,\ZipArchive::CREATE|\ZipArchive::OVERWRITE) === true) {
foreach ($content as $val){
if(isset($val['data'])){
$file_path = public_path().$val['data'];
if(file_exists($file_path)){
$info = pathinfo($file_path);
$zip->addFile($file_path,$info['basename']);
$count++;
}
}
}
}
$zip->close();
return response()->download($filename);
本作品採用《CC 協議》,轉載必須註明作者和本文連結