您的位置:首页 > 编程语言 > PHP开发

laravel 打包成zip并下载

2020-01-15 11:07 3095 查看
//初始化zip 名字
$zip_file = 'Example.zip';
$zip = new \ZipArchive();
$zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
//将被压缩文件夹
$path = storage_path('/app/files/');
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
foreach ($files as $name => $file)
{
// 我们要跳过所有子目录
if (!$file->isDir()) {
$filePath     = $file->getRealPath();
// 用 substr/strlen 获取文件扩展名
$relativePath = 'invoices/' . substr($filePath, strlen($path) + 1);
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
return response()->download($zip_file);
  • 点赞
  • 收藏
  • 分享
  • 文章举报
lwlzq 发布了19 篇原创文章 · 获赞 3 · 访问量 1146 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: