Add photos together in Add in the Data controller
welfare
welfare 2017-08-08 11:14:48

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

welfare
welfare

reply all(3)
糊涂斌
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('上传成功');
            }
        }
    }
}


大家讲道理

TP请参考tp手册中上传图片处理的文档,写的非常详细

  • reply 看的不太懂!就拿我这个怎么把上传图片页写进去?
    welfare author 2017-08-08 15:47:08
ringa_lee

在add这个方法里,调用图片上传的类,然后把上传好的图片路径保存到数据库就行了

  • reply 但是数据库是空的,没存进去。如果你有空能帮我下吗?QQ:9 3 2 6 9 6 1 8 1
    welfare author 2017-08-08 15:46:02
  • reply 你图片上传完了,能得到图片的名称吗?可以图片的名称和路径那就肯定可以存到数据库的啊
    ringa_lee author 2017-08-08 16:37:16
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!