search

Home  >  Q&A  >  body text

Add photos together in Add in the Data controller

How to write the image upload code in the add in the Data controller! I can't add it according to the previous method! Can you give me a sample code? How should I add pictures to upload here? Please help

TIM截图20170808111414.png

welfarewelfare2697 days ago1292

reply all(3)I'll reply

  • 糊涂斌

    糊涂斌2017-08-16 17:59:05

    public function upload(){
        $upload_img=M('upload_img');//上传文件路径写入的表
        if(!empty($_FILES)){
            //上传单个图像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     1*1024*1024 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =      './upload/'; // 设置附件上传根目录
            $upload->savePath  =      date('Y-m-d'); // 设置附件上传(子)目录
            $upload->saveName=array('uniqid','');//上传文件的保存规则
            $upload->autoSub  = true;//自动使用子目录保存上传文件 
            $upload->subName  = array('date','Ymd');
            // 上传单个图片
            $info   =   $upload->uploadOne($_FILES['image']);
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{// 上传成功 获取上传文件信息
                $img_url=$info['savepath'].$info['savename'];
                $data['img_url']=$img_url;
                $data['img_name']=$info['savename'];
                $data['create_time']=time();
                $upload_img->create($data);
                $result=$upload_img->add();
                if(!$result){
                    $this->error('上传失败!');
                }else{
                    $this->success('上传成功');
                }
            }
        }
    }


    reply
    1
  • 大家讲道理

    大家讲道理2017-08-08 14:14:59

    TP Please refer to the document for uploading image processing in the TP manual, it is written in very detail

    reply
    0
  • welfare

    I don’t quite understand! How do I put this into the upload image page?

    welfare · 2017-08-08 15:47:08
  • ringa_lee

    ringa_lee2017-08-08 14:14:26

    In the add method, call the image upload class, and then save the uploaded image path to the database

    reply
    0
  • welfare

    But the database is empty and not saved. If you have time, can you help me? QQ:9 ​​3 2 6 9 6 1 8 1

    welfare · 2017-08-08 15:46:02
    ringa_lee

    After uploading your picture, can you get the name of the picture? If you can provide the name and path of the image, it will definitely be saved in the database.

    ringa_lee · 2017-08-08 16:37:16
  • Cancelreply