laravel 实现文件夹下所有文件 打包zip下载
作者:互联网
开启ZIP扩展
$dir = 'bai_file/'.$date; if($dir){ $path = public_path($dir); $zip_name = public_path($dir.'/'.date('Y-n').'.zip'); $zip = null; $count = 0; //迭代器 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)); foreach ($files as $name => $file){ //排除文件夹 if (!$file->isDir()) { //是否实例化了ZipArchive对象, if(!$zip) { $zip = new \ZipArchive(); //打开zip包 $zip->open($zip_name, \ZipArchive::CREATE | \ZipArchive::OVERWRITE); } //获取文件的绝对路径 $filePath = $file->getRealPath(); //把文件添加的zip包中 $zip->addFile($filePath, $file->getFilename()); $count++; } } if($zip){ //关闭zip包 $zip->close(); } if($count){ return response()->download($zip_name); }else{ return response('', 404); } }else{ return response('', 404); }
标签:laravel,name,zip,ZipArchive,文件夹,file,path,dir 来源: https://www.cnblogs.com/zjj1990/p/16396231.html