>  기사  >  php教程  >  @apiDescription 批量下载

@apiDescription 批量下载

WBOY
WBOY원래의
2016-07-06 13:28:331084검색
跳至 [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);
        }
    }
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.