@apiDescription 批量下载
程序员文章站
2022-05-02 23:26:53
...
跳至
[1]
[全屏预览]
public function download() { $user = \Auth::user(); //验证字段 $request = array_filter($this->request->input()); array_walk($request, function (&$value, $key) { if (is_array($value)) { $value = array_filter($value); } }); $validator = \Validator::make($request, [ 'attachment_ids' => 'required|array|min:1', ]); if ($validator->fails()) { return $this->errorBadRequest($validator->messages()->all()); } $attachmentIds = $this->request->get('attachment_ids'); $attachments = Attachment::whereIn('id', $attachmentIds)->get(); if (!$attachments->count()) { return $this->response->errorNotFound(); } if ($attachments->count() == 1) { $attachment = $attachments->first(); if (!$attachment->allowDownload($user)) { return $this->response->errorForbidden(); } return response()->download(public_path($attachment->relative_path)); } else { $zip = new \ZipArchive(); $zipFile = storage_path('app/cache/' . date('Ymdhis') . uniqid() . '.zip'); $zip->open($zipFile, \ZIPARCHIVE::CREATE); foreach ($attachments as $attachment) { if (!$attachment->allowDownload($user)) { continue; } $filename = basename($attachment->relative_path); $zip->addFile($attachment->relative_path, $filename); } $zip->close(); return response()->download($zipFile); } }
上一篇: php快速url重写更新版[需php 5.30以上
下一篇: PHP写的多服务器文件同步的程序
推荐阅读
-
影音先锋怎么设置保存路径?影音先锋下载位置更改教程
-
SystemVue 2017安装破解详细图文教程(附破解文件下载)
-
asp.net实现服务器文件下载到本地的方法
-
Cadence SPB OrCAD Allegro 17.2安装破解详细图文教程(附破解文件下载)
-
UG NX9.0正式版安装破解教程图文详解(含ug9.0破解版下载地址)
-
UG NX 11.0正式版安装破解图文详细教程(附破解版下载地址)
-
Sibelius怎么安装?西贝柳斯Sibelius 2018中文破解安装教程(附破解补丁下载)
-
会声会影素材去哪下载 会声会影x5素材下载方法
-
会声会影x6怎么下载安装好莱坞转场?
-
cadence17.4 下载安装教程图解