//初始化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并下载
PHP文件压缩与下载
最新推荐文章于 2024-04-22 19:55:52 发布
本文介绍了一种使用PHP进行文件夹压缩并将其下载为ZIP文件的方法。通过初始化ZIP文件,利用ZipArchive类创建和写入文件,遍历指定路径下的所有文件,并添加到ZIP中,最后关闭ZIP并提供下载。
549

被折叠的 条评论
为什么被折叠?



