use GuzzleHttp\Client;
//打包下载
function downLoadPackage($fileData,$filePackage='document',$zipFileName='file'){
//存到 域名指向的public 目录
//1.先把远程文件下载到本地文件夹中
// $fileData = [
// "http://xxxxx/files/upload/123.doc",
// "http://xxxxx/files/upload/123.txt",
// "http://xxxxx/files/upload/123.xlsx",
// "http://xxxxx/files/upload/123.png",
// "http://xxxxx/files/upload/123.jpg",
// "http://xxxxx/files/upload/123.jpeg",
// ];
$client = new Client(['verify' => false]); //忽略SSL错误
$judge = false; //判断是否保存完毕
foreach ($fileData as $val) {
$file_temp_path = $filePackage . '/' . pathinfo($val)['basename']; //保存文件地址+原始文件名
$tempData = $client->request('get', $val)->getBody()->getContents();
$judge = Storage::disk('base_public')->put($file_temp_path, $tempData);//文件保存地址
}
if ($judge) {
//2.打包下载本地文件夹
//初始化zip 名字
$zipName = $zipFileName . '.zip';
$zip_file = str_replace("\\", '/', base_path('public/'.$filePackage.'/')) . $zipName; //public/document/file.zip
$zip = new \ZipArchive();
$zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
//将被压缩文件夹
$path = str_replace("\\", '/', base_path('public/'.$filePackage.'/')); //public/document/remoteFiles
//迭代器
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
foreach ($files as $file) {
//跳过所有子目录
if (!$file->isDir()) {
//获取文件路径
$filePath = $file->getRealPath();
//获取文件名
$fileName = $file->getFilename();
$zip->addFile($filePath, $fileName);
}
}
$zip->close();
return config('app.back_url').$filePackage.'/' . $zipName;
//return response()->download($zip_file, $zipName); //下载生成的zip文件
}
//调用
downLoadPackage($fileData,'gauge/','123');//保存zip文件