tp5 上传处理类

         tp5和tp3文件上传区别有些大,tp5上传文件官方文档讲解的比较模糊,自己也是看了好久的源码才得出个所以然,由此和大家分享一下,以免重复踩我踩过得坑。

分为两类,一类是文件处理,一类是图片处理。

isAjax()){
            $input_name  =  input('get.name');
            $path        =  ROOT_PATH . 'public' . DS . 'admin' .DS. 'upload' . DS . input('get.path');
            $url_path    = 'admin' .DS. 'upload' . DS . input('get.path');
            $type        =  input('get.type');
            $file        =  request()->file($input_name);

            if ($type=='img'){
                return $this->imgHandle($file, $path, $url_path);
            }

            if($type=='file'){
                return $this->fileHandle($file, $path, $url_path);
            }

        }
        exit('非法访问!');

    }

    private function fileHandle($file , $path, $url_path){

        $checkExt = $file->validate(['ext'=>input('get.ext')])->check();
        if ($checkExt){
            $checkSize =  $file->validate(['size'=>2*1024*1024])->check();
            if ($checkSize){
                $info = $file->rule('uniqid')->move($path);
                if ($info){
                    $file_name= $info->getFilename();
                    return json(['status'=>1, 'path'=> $url_path . DS . $file_name, 'sucess'=>'上传成功']);
                }else{
                    return json(['status'=>0, 'error'=>'上传失败请联系管理员']);
                }
            }else{
                return json(['status'=>0, 'error'=>'PDF文件大小不得超过2M']);
            }
        }else{
            return json(['status'=>0, 'error'=>'请上传pdf文件']);
        }
    }


    //图片处理
    private  function imgHandle($file, $path, $url_path){
        //检查是否为图片
        $checkImg = $file->validate(['ext'=>'jpg,png,gif,jpeg'])->check();
        if ($checkImg){

            $width     =   input('get.with');
            $height    =   input('get.height');
            $image     =   Image::open($file);
            $mime      =   explode('/', $image->mime())[1];
            $file_name =   date('YmdHis').'.'.$mime;

            //限制图片大小
            $checkSize =  $file->validate(['size'=>2*1024*1024])->check();
            if($checkSize){
                if (empty($width) || empty($height)){
                    $image->save($path. DS . $file_name);
                    return json(['status'=>1, 'path'=> $url_path . DS . $file_name, 'sucess'=>'上传成功']);
                }else{
                    $image->thumb($width, $height,Image::THUMB_FIXED)->save($path. DS . $file_name);
                    return json(['status'=>1, 'path'=> $url_path . DS . $file_name ,'sucess'=>'上传成功']);
                }

            }else{
                return json(['status'=>0, 'error'=>'图片大小不得超过2M']);
            }
        }else{
            return json(['status'=>0, 'error'=>'请上传图片']);
        }

    }

    /**
     * 删除文件
     * @return \think\response\Json
     */
    public function deleteFile(){

        if(request()->isAjax()){
            $img_url  = input('post.img_url');
            $file_path= ROOT_PATH.'public'. DS . $img_url;
            if (is_file($file_path)){
                if(unlink($file_path)){
                    return json(['status'=>1, 'success'=>'删除成功']);
                }else{
                    return json(['status'=>0, 'error'=>'删除失败,请联系管理员']);
                }
            }

            return json(['status'=>0, 'error'=>'文件不存在,请联系管理员']);

        }else{
            exit('非法访问!');
        }
    }




}


你可能感兴趣的:(tp5 上传处理类)