当前位置:Gxlcms > PHP教程 > mysql-tinkphp3.2中form表单的上传和文件上传不能同步

mysql-tinkphp3.2中form表单的上传和文件上传不能同步

时间:2021-07-01 10:21:17 帮助过:7人阅读

在form表单中

这两个表头 是Controller 控制器中的
//写的是 form表单中 input 平常数据的上传 和 添加数据库 的操作

function tjkh(){
        $user = M('customer');
        $uid = session('userid');
        print_r($uid);
        if(isset($_POST['act']) && $_POST['act'] == 'tjkh'){
          $sel1 = I('post.type');
          $name = I('post.xingm');
          $phone = I('post.phone');
          $age = I('post.age');
          $marriage = I('post.marriage');
          $cre = I('post.cre_repoting');
          $water = I('post.water');
          $lines = I('post.lines');
          $lins = I('post.license');
          $manager = I('post.manager');
          $spe = I('post.species');
          $radio =I('post.radio');
          $note =I('post.note');
          $time = time();
          $nm = date("Y-m-d",$time);
          $upload = I("post.file");
          //构建写入的数据数组 
          $data['type'] = $sel1;
          $data['name'] = $name;
          $data['phone'] = $phone;
          $data['age'] = $age;
          $data['marriage'] = $marriage;
          $data['cer_repoting'] = $cre;
          $data['income'] = $water;
          $data['lines'] = $lines;
          $data['license'] = $lins;
          $data['manage'] = $manager;
          $data['species'] = $spe;
          $data['collecting'] = $radio;
          $data['note'] = $note;
          $data['time'] = $nm;    
          $data['upload'] = $upload;
          $data['user_id'] = $uid;
          if($name != ""){
              if($phone != ""){
                  if($age != ""){
                     if($water != ""){
                         if($lines != ""){
                             if($note != ""){
                                 if($user->add($data)){
                                   $this->redirect('Home/zhuye');
                                }else{
                                    $this->error('注册失败7');
                                }
                            }else{
                                  $this->error('注册失败6');
                            }
                        }else{
                              $this->error('注册失败5');
                        }
                    }else{
                          $this->error('注册失败4');
                    }
                }else{
                      $this->error('注册失败3');
                }
            }else{
                  $this->error('注册失败2');
            }    
          }else{
              $this->error('注册失败1');
          }    
        }
        $this->display('Home/tjkh');
    }


// 写的是 form表单中 上传文件的操作
public function upload(){

        $upload = new \Think\Upload();// 实例化上传类    
        $upload->maxSize   =     2*1024*1024 ;// 设置附件上传大小    
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','txt','doc','wps','html');// 设置附件上传类型    
        $upload->savePath  =      './Uploads/'; // 设置附件上传目录    
        $upload->autoSub    =   true;   
        $upload->subName    =   array('date','Ymd');
        $upload->replace    =   true;
        // 上传文件     
        $info   =   $upload->upload();   
        
        if(!$info) {// 上传错误提示错误信息        
        $this->error($upload->getError());    
        }else{// 上传成功        
          foreach($info as $file){
                  $file['savepath'].$file['savename']; 
          }
          $this->success('上传成功!');
        }
        $user = M('up_file');
        $userid = session('userid');
        $time = time();
        $nm = date("Y-m-d H:i:s",$time);
        $upload = $info['file']['name'];
    
        // 保存当前数据对象
        $data['file_img'] = $file['savepath'].$file['savename'];
        $data['user_id'] = $userid;
        $data['file_time'] = $nm;
        $data['upload'] = $upload;
        if($userid != ""){
            $user->add($data);    
            }
    }

相对应的 两个方法 怎么能让 这两个方法同步
而不是只能实现一个方法

回复内容:

在form表单中

这两个表头 是Controller 控制器中的
//写的是 form表单中 input 平常数据的上传 和 添加数据库 的操作

function tjkh(){
        $user = M('customer');
        $uid = session('userid');
        print_r($uid);
        if(isset($_POST['act']) && $_POST['act'] == 'tjkh'){
          $sel1 = I('post.type');
          $name = I('post.xingm');
          $phone = I('post.phone');
          $age = I('post.age');
          $marriage = I('post.marriage');
          $cre = I('post.cre_repoting');
          $water = I('post.water');
          $lines = I('post.lines');
          $lins = I('post.license');
          $manager = I('post.manager');
          $spe = I('post.species');
          $radio =I('post.radio');
          $note =I('post.note');
          $time = time();
          $nm = date("Y-m-d",$time);
          $upload = I("post.file");
          //构建写入的数据数组 
          $data['type'] = $sel1;
          $data['name'] = $name;
          $data['phone'] = $phone;
          $data['age'] = $age;
          $data['marriage'] = $marriage;
          $data['cer_repoting'] = $cre;
          $data['income'] = $water;
          $data['lines'] = $lines;
          $data['license'] = $lins;
          $data['manage'] = $manager;
          $data['species'] = $spe;
          $data['collecting'] = $radio;
          $data['note'] = $note;
          $data['time'] = $nm;    
          $data['upload'] = $upload;
          $data['user_id'] = $uid;
          if($name != ""){
              if($phone != ""){
                  if($age != ""){
                     if($water != ""){
                         if($lines != ""){
                             if($note != ""){
                                 if($user->add($data)){
                                   $this->redirect('Home/zhuye');
                                }else{
                                    $this->error('注册失败7');
                                }
                            }else{
                                  $this->error('注册失败6');
                            }
                        }else{
                              $this->error('注册失败5');
                        }
                    }else{
                          $this->error('注册失败4');
                    }
                }else{
                      $this->error('注册失败3');
                }
            }else{
                  $this->error('注册失败2');
            }    
          }else{
              $this->error('注册失败1');
          }    
        }
        $this->display('Home/tjkh');
    }


// 写的是 form表单中 上传文件的操作
public function upload(){

        $upload = new \Think\Upload();// 实例化上传类    
        $upload->maxSize   =     2*1024*1024 ;// 设置附件上传大小    
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','txt','doc','wps','html');// 设置附件上传类型    
        $upload->savePath  =      './Uploads/'; // 设置附件上传目录    
        $upload->autoSub    =   true;   
        $upload->subName    =   array('date','Ymd');
        $upload->replace    =   true;
        // 上传文件     
        $info   =   $upload->upload();   
        
        if(!$info) {// 上传错误提示错误信息        
        $this->error($upload->getError());    
        }else{// 上传成功        
          foreach($info as $file){
                  $file['savepath'].$file['savename']; 
          }
          $this->success('上传成功!');
        }
        $user = M('up_file');
        $userid = session('userid');
        $time = time();
        $nm = date("Y-m-d H:i:s",$time);
        $upload = $info['file']['name'];
    
        // 保存当前数据对象
        $data['file_img'] = $file['savepath'].$file['savename'];
        $data['user_id'] = $userid;
        $data['file_time'] = $nm;
        $data['upload'] = $upload;
        if($userid != ""){
            $user->add($data);    
            }
    }

相对应的 两个方法 怎么能让 这两个方法同步
而不是只能实现一个方法

在tjkh()方法中调用upload()方法,在upload()里返回保存的路径

人气教程排行