tp6 分片上传

//分片上传
    public function uploadFile()
    {
        if ($this->request->isGet()) {
            $code = md5(uniqid()); // 文件的唯一编码
            return $this->apiResult(200, '文件唯一码', ['code' => $code]);
        } elseif ($this->request->isPost()) {
            try {
                $file = $this->request->file('file');  //文件
                $code = $this->request->post('code', ''); //唯一码
                $sort = $this->request->post('sort', ''); //片数
                $tag = $this->request->post('tag', '');   //默认2, 1是最后一片
                $mark = $this->request->post('mark', ''); //1普通上传 2 分片上传
                $ext = $this->request->post('ext', '');   //文件后缀
                if ($mark == 1) {
                    if ($file) {
                        $folder = '../public/upload/' . date('Ymd');
                        if (!file_exists($folder)) mkdir($folder, 0777, true);
                        if (file_exists($folder)) {
                            $file->move($folder . '/', $file->getOriginalName());
                        }
                        $image_url = '/upload/' . date('Ymd') . '/' . $file->getOriginalName();
                        return $this->apiResult(200, '普通上传成功', ['file_url' => $image_url]);
                    } else {
                        return $this->apiResult(403, '普通上传失败,缺少上传参数', []);
                    }
                } else if ($mark == 2) {
                    if ($file && $code && $sort && $tag) {
                        $dir = '../public/upload/' . $code;
                        if (!file_exists($dir)) mkdir($dir, 0777, true);
                        if (!file_exists($dir . '/' . $sort . '.tmp')) {
                            $file->move($dir . '/', $sort . '.tmp');
                        }
                        if ($tag == 1 && $ext) {
                            $save_file = '../public/upload/'. date('Ymd') .'/';
                            $file_name = $code . '.' . $ext;
                            for ($i = 1; $i <= $sort; $i++) {
                                $str = file_get_contents($dir . '/' . $i . '.tmp');
                                file_put_contents($save_file . $file_name, $str, FILE_APPEND);
                                unlink($dir . '/' . $i . '.tmp');
                            }
                            rmdir($dir);
                            $file_url = env('IMAGE_URL.DNS') . '/upload/' . date('Ymd') .'/'. $file_name;
                            $file_size = filesize($save_file . $file_name);
                            return $this->apiResult(200, '分片上传成功', ['file_url' => $file_url, 'file_size' => $file_size]);
                        }
                        return $this->apiResult(200, '上传成功');
                    }
                    return $this->apiResult(400, '文件上传失败,缺少上传参数');
                }
                return $this->apiResult(400, '上传类型错误');
            } catch (\Exception $e) {
                return $this->apiResult(400, $e->getMessage());
            }
        }
        return $this->apiResult(300, '请求方式错误');
    }

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值