tp5.0图片上传

 public function upload_file() {
        try {
            $this->regapi([
                'f_server_name' => '外部调用-上传图片',
                'f_server_url' => 'xxxx/frame/apis/upload_file',
                'f_server_code_type' => '1',
                'f_server_port' => '80',
            ]);

            //获取上传文件信息;
            $files = request()->file('image');
            if (empty($files)) {
                $e = new apis_exception();
                $e->seterror(607);
                throw  $e;
            }
            $filePaths = ROOT_PATH . 'public' . DS . 'uploads';
            if (!file_exists($filePaths)) {
                mkdir($filePaths, 0777);
            }
            foreach($files as $file){
                $info = $file->move($filePaths);
                if (empty($info)) {
                    $e = new apis_exception();
                    $e->seterror(606);
                    throw  $e;
                }
                $imgpath = ROOT_PATH . 'public' . DS . 'uploads/' . $info->getSaveName();
                $image = \think\Image::open($imgpath);
                $date_path = ROOT_PATH . 'public' . DS . 'uploads';
                if (!file_exists($date_path)) {
                    mkdir($date_path, 0777, true);
                }
                $thumb_path = $date_path . '/' . $info->getFilename();
                $image->thumb(120, 120)->save($thumb_path);
                
                $path = toolapp::toolOss($dir, $imgpath);
                $smallPath = toolapp::toolOss($dir, $thumb_path);
                if(empty($path) ||empty($smallPath)){
                    $e = new apis_exception();
                    $e->seterror(605);
                    throw  $e;
                }
                //数据入库;
                $data = [
                    'f_image_path' => $path['url'],//上传原图
                    'f_small_image_path' => $smallPath['url'],//上传缩略图
                    'f_image_add_time' => time(),        //添加时间
                    'f_image_up_time' => time(),        //更新时间
                    'f_image_add_operator' => $this->php->common_user->get_uid(),//操作人
                ];
                //返回给前端数据;
                $arr=[
                     'image'=>$path['url'],
                     'thumb'=>$smallPath['url'],
                ];
                unset($info);//清除文件,非常重要;
                @unlink($thumb_path);
                @unlink($imgpath);//删除文件
                rmdir('uploads/' . date('Ymd', time()));//删除文件夹
                $image_info[]=$data;
                $arr2[]=$arr;
            }

            //批量插入数据库;
            Db::name(dn::image_image)->insertAll($image_info);
            //返回数据;
            return [
                'status' => true,
                'msg' => '上传成功!',
                'data' => $arr2
            ];
        }catch (appexception $e) {
            $e->handle();
        }
    }

 

转载于:https://www.cnblogs.com/wangfenphph2/p/9041419.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值