thinkphp upload类 图片上传到本地 生成缩略图 再 ftp上传

  public function upload(){
         if(intval(session("userid"))==0)
        {    
            header('Content-type: text/json');
            header('HTTP/1.1 401 error');
            echo json_encode(array("success" => false,"code"=> 1001,"msg" => "请先登录!","obj"=> null,"map"=> null,"list"=> null));
            exit;
        }
        if(I('get.fileType',0)==2){
            $ftpconf = C('UPLOAD_VIDEO_TYPE_CONFIG');//上传到音频服务器
        }
        else{
            $ftpconf = C('UPLOAD_TYPE_CONFIG');//上传到图片服务器
        }
        if(I('get.fileType',0)==2)
        {
            $upload->exts = array('mp3');// 设置附件上传类型
            $upload->savePath = date('Y',time()).'/'.date('m',time()).'/'.date('d',time()).'/'.session("userid").'/'; // 设置附件上传(子)目录
            $upload->subName  = '';
        }
        if(I('get.fileType',0)!=2){
                if(I('get.fileType',0)==0){
                    $savePath="backImg/";//背景图
                }else{
                    $savePath="pic/";//封面图
                }
                //*********上传本地背景图              
                $upload2 = new \Think\Upload();// 实例化上传类
                $upload2->maxSize = 3145728 ;// 设置附件上传大小
                $upload2->exts = array('jpg', 'gif', 'png', 'jpeg');
                $upload2->rootPath = './Uploads/';
                $upload2->savePath = $savePath.session("userid").'/';
                $upload2->subName  = array('date','Ym');
                $upload2->saveName = 'uniqid';
                $info2 = $upload2->upload();
                //***ftp背景图            
                $upload = new \Think\Upload($ftpconf,'',$ftpconf);
                $upload->maxSize = 3145728 ;        
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->subName  = array('date','Ym');
                $upload->savePath = 'h5/'.$savePath.session("userid").'/';
                $upload->rootPath = $ftpconf['rootPath'];
                $upload->saveName = 'uniqid';
                // 采用GUID序列命名 $upload->saveName = 'guid';
                $info = $upload->upload(); //上传到FTP图片服务器
        }else{
            //将音乐上传到本地
            $upload2 = new \Think\Upload(array(),'LOCAL');// 实例化上传类
            $upload2->maxSize = 3145728 ;// 设置附件上传大小
            $upload2->exts = array('mp3');// 设置附件上传类型
            $upload2->savePath = date('Y',time()).'/'.date('m',time()).'/'.date('d',time()).'/'.session("userid").'/'; // 设置附件上传(子)目录
            $upload2->rootPath = './Uploads/'; // 设置附件上传根目录
            $upload2->subName  = '';
            $fileftpname = str_replace('.'.$info['file']['ext'],'',$info['file']['savename']);
            $upload2->saveName = $fileftpname;
            $info2 = $upload2->upload();
        }
        
        if(!$info || !$info2) {// 上传错误提示错误信息
            header('Content-type: text/json');
            header('HTTP/1.1 401 error');
            echo json_encode(array("success" => false,"code"=> 1001,"msg" => "文件上传错误!","obj"=> null,"map"=> null,"list"=> null));
             $this->error($upload->getError());
            exit;
        }else{// 上传成功 获取上传文件信息
            header('Content-type: text/json');
            header('HTTP/1.1 200 ok');
            if(I('get.fileType',0)!=2){
                $info = $info2;//生成缩略图必须是本地文件
            }
            foreach($info as $file){
                if(I('get.fileType',0)!=2)
                {
                    $image = new \Think\Image();
                    $thubimage = $upload2->rootPath.$file['savepath'].$file['savename'];
                    
                    //缩略图名字
                    $thubimagename = str_replace(".".$file['ext'],"_thumb.".$file['ext'],$file['savename']);
                    //本地缩略图/Uploads/pic/1/201603/532_thumb.jpg
                    $thubimagenew =$upload2->rootPath.$file['savepath'].$thubimagename;
                    $image->open($thubimage);
                    if(I('get.fileType',0)==0)
                    {
                        $image->thumb(80, 126)->save($thubimagenew);
                    }
                    else
                    {
                        $image->thumb(80, 80)->save($thubimagenew);
                    }

                    //将生成的缩略图上传到图片服务器
                    $ftpuser = $ftpconf['username'];
                    $ftppass = $ftpconf['password'];
                    $ftphost = $ftpconf['host'];
                    $ftpport = $ftpconf['port'];
                    $rootPath = $ftpconf['rootPath'];
                    $ftptimeout = $ftpconf['timeout'];
                    //链接FTP服务器
                    $link = ftp_connect($ftphost, $ftpport, $ftptimeout);
                    //ftp保存缩略图地址
                    $ftpthubimagenew=str_replace("Uploads","h5", $thubimagenew);
                    if($link){
                        ftp_login($link, $ftpuser, $ftppass);
                        $putinfo = ftp_put($link,$ftpthubimagenew,$thubimagenew,FTP_BINARY);
                        if (!$putinfo){
                             echo '{"success":false,"code":101,"msg":"操作失败","obj":null,"map":null,"list":null}';
                        }
                    }
                    ftp_close($link);
                }
                $sizeint = intval($file['size']/1024);
                $jsonstr = '{"success":true,"code":200,"msg":"success","obj":{"id":9386090,"name":"'.$file['savename'].'","extName":"'.strtoupper($file['ext']).'","fileType":0,"bizType":0,"path":"'.$file['savepath'].$file['savename'].'","tmbPath":"'.$thubimagenew.'","createTime":1426209412922,"createUser":"'.session("userid").'","sort":0,"size":'.$sizeint.',"status":1},"map":null,"list":null}';
                

                
                $model = M('upfile');
                // 取得成功上传的文件信息
                // 保存当前数据对象
                $data['ext_varchar'] = strtoupper($file['ext']);
                $data['filename_varchar'] = $file['name'];
                $data['filetype_int'] = I('get.fileType',0);
                $data['biztype_int'] = I('get.bizType',0);
                $data['userid_int'] = session("userid");
                $data['filesrc_varchar'] = $file['savepath'].$file['savename'];
                $data['sizekb_int'] = $sizeint;
                if(I('get.fileType',0)==1)//封面图路径字段
                $thubimagenew=$file['savepath'].$thubimagename;
                $data['filethumbsrc_varchar'] =$thubimagenew;
                $data['create_time'] = date('y-m-d H:i:s',time());
                $model->add($data);
                echo $jsonstr;
            }
        }
    }

转载于:https://my.oschina.net/ndczds/blog/639032

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值