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
糊涂斌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('上传成功'); } } } }
大家讲道理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
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