laravel 打包成zip并下载

		//初始化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);
发布了21 篇原创文章 · 获赞 7 · 访问量 1418

猜你喜欢

转载自blog.csdn.net/weixin_37647596/article/details/103027424