批量打包下载时,出现文件不存在
想用ZipArchive实现批量打包下载, 运行测试代码如下:
public function download()
{
$zip_file = "files.zip"; // 要下载的压缩包的名称
$files = array([
'name'=>'develop',
'path' => '/file/kI9iQYZElfSBDwxGAH5ARQV2Fu89pzPkyIkrqROc.docx'
]);
if(!file_exists($zip_file)) {
// 初始化 PHP 类
$zip = new \ZipArchive();
$zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
foreach ($files as $file) {
$old_path = $file['path'];
$new_path = str_replace('/', '\\', $old_path);
// 所以,它将在 ZIP 中创建另一个名为 "storage/" 的路径,并把文件放入目录。
$zip->addFile(public_path($new_path), $file['name'] . $extension);
}
$zip->close();
// 我们将会在文件下载后立刻把文件返回原样
$down_files = response()->download($zip_file,"test.zip");
}
// return $down_files;
dd('done');
}
版本信息:laravel 7.x / php 7.3.4nts
最终结果 The file "files.zip" does not exist
, 此类程序以前没写过, 实在找不出原因,帮我看一下,是不是哪里出问题了,万分感谢!
推荐文章: