Rumah > php教程 > PHP源码 > teks badan

@apiDescription 批量下载

WBOY
Lepaskan: 2016-07-06 13:28:33
asal
1113 orang telah melayarinya
跳至 [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);
        }
    }
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan