//初始化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);
laravel 打包成zip并下载
最新推荐文章于 2024-04-22 19:55:52 发布