tp中附件上传文件,表单提交

   public function tianjia(){
             $goods=D('Goods');
             if(!empty($_POST)){
                   if($_FILES['f_goods_image']['error']<4){
                          $cfg=array( 
                             'rootPath' =>  './Public/uploadss/', 
                                 );//保存根路径
                     $up=new ThinkUpload($cfg); //uploadOne方法执行成功后会把附件(在服务器上)的名字和路径等相关信息给我们返回。
                     $z= $up->uploadOne($_FILES['f_goods_image']); //  dump($up->getError()); //dump($z);//把上传好的附件上传到数据库里面
                     $bigimg= $_POST['goods_big_img']=  $up->rootPath .$z['savepath'].$z['savename'];    //大图路径名
                     $smallimg=$up->rootPath.$z['savepath'].'small_'.$z['savename'];                     //小图路径名
                    //对上传好的图片制作缩略图
                    $im=new ThinkImage();//实例化image对象
                    $im->open($bigimg);//打开被处理的图片
                    $im->thumb(100, 100, 1);//制作缩略图(默认1为等比例缩放对象)
                    $im->save($smallimg);//保存缩略图到服务器
                 }
                 //收集表单  
                 $shuju=$goods->create();
                 $z=$goods->add($shuju);
                 if($z){
                     $this->redirect('showlist',3, '添加商品成功'); 
                 }  else {
                     $this->redirect('tianjia',3,'添加商品失败');    
                 }
             }  else {
                 //展示表单
                     $this->display(); 
             }
         }
   public function uploads(){ //文件上传方法
            if(!empty($_FILES)){
                $upload = new ThinkUpload();// 实例化上传类
                $upload->maxSize   = 3145728 ;// 设置附件上传大小
                $upload->exts      =array('jpg', 'gif', 'png', 'jpeg','bmp');// 设置附件上传类型
                $upload->rootPath='./';//上传根目录
                $upload->savePath  ='./Public/uploads/'; // 设置附件上传目录
                // 上传文件
                $info  =   $upload->upload();

                if(!$info) {// 上传错误提示错误信息
                    $info=$upload->getError();
                    $this->ajaxReturn($info,'json');
                }else {
                    // 上传成功

                    $data['url']=$info['file']['savepath'].$info['file']['savename'];
                    $data['status']=0;
                    $this->ajaxReturn($data,'json');
                }
            }
            $this->ajaxReturn("不存在",'json');

        }
原文地址:https://www.cnblogs.com/kangshuai/p/5663035.html