thinkphp处理数据库的数据

展开阅读全文

thinkphp中怎么处理上传文件

10-22

thinkphp中怎么处理上传文件,也即要处理所有文件。自己做了一个网站要上传所有的文件 包括图片、txt、doc、视频等等。如何上传 插入数据库里面呀?rn$m = M("file");rn //$_clean = array();rn if (!empty($_FILES))rn $im = $this->_upload();rn $_POST["filename"] = $im;rn $_POST["uptime"] = time();rn rn rnrn $n = $m->add();rn if ($n)rn $str = "添加成功";rn else rn $str = "添加失败";rn rn $this->assign("str",$str);rn $this->display("upload");rn rn rn // 文件上传 rn protected function _upload() rn import("@.ORG.UploadFile"); rn $upload = new UploadFile(); rn //设置上传文件大小 rn $upload->maxSize = 3292200; rn //设置上传文件类型 rn $upload->allowExts = explode(',', 'jpg,gif,png,jpeg,pjpeg,bmp,doc,rar,txt'); rn //设置附件上传目录 rn $upload->savePath = './Public/upload/'; rn //设置上传文件规则 rn $upload->saveRule = uniqid;rn if (!$upload->upload()) rn //捕获上传异常 rn $this->error($upload->getErrorMsg()); rn else rn //取得成功上传的文件信息 rn $uploadList = $upload->getUploadFileInfo(); rn //import("@.ORG.Image"); rn //给m_缩略图添加水印, Image::water('原文件名','水印图片地址') rn /*Image::water($uploadList[0]['savepath'].'m_'.$uploadList[0]['savename'], '/ThinkPHP_2.1_full_with_extend/Examples/File/Tpl/default/Public/Images/logo2.png'); */rn return $uploadList[0]['savename']; rn rn rn 我这样处理不行哦。 论坛

thinkphp多图上传插入数据库

12-20

这是原来的两张表:表1 表2rn[img=https://img-bbs.csdn.net/upload/201512/20/1450597171_394007.png][/img]rn[img=https://img-bbs.csdn.net/upload/201512/20/1450596345_653855.png][/img]rn这是新的表 表3:rn[img=https://img-bbs.csdn.net/upload/201512/20/1450596492_881576.png][/img]rn希望把图片上传样式如表3rn下面是原来图片上传写法:(分表插入)rnPictureMode.class.phprn[code=php]rn $file) rn $ext = strtolower($file['ext']);rn if(in_array($ext, array('jpg','jpeg','bmp','png')))rn hook('dealPicture',$file['tmp_name']);rn rn rn $info = $Upload->upload($files);rnrn if($info) //文件上传成功,记录文件信息rn foreach ($info as $key => &$value) rn /* 已经存在文件记录 */rn if(isset($value['id']) && is_numeric($value['id']))rn continue;rn rn /* 记录文件信息 */rn if(strtolower($driver)=='sae')rn $value['path'] = $config['rootPath'].'Picture/'.$value['savepath'].$value['savename']; //在模板里的url路径rn elsern if(strtolower($driver) != 'local')rn $value['path'] =$value['url'];rn rn elsern $value['path'] = (substr($setting['rootPath'], 1).$value['savepath'].$value['savename']); //在模板里的url路径rn rn rn $value['type'] = strtolower($driver);rnrn if($this->create($value) && ($path = $this->add()))rn $value['path'] = $path;rn else rn //TODO: 文件上传成功,但是记录文件信息失败,需记录日志rn unset($info[$path]);rn rn rn foreach($info as &$t_info)rn if($t_info['type'] =='local')rn $t_info['path']=fixAttachUrl($t_info['path']);rn rn elsern $t_info['path']=$t_info['path'];rn rn rn return $info; //文件上传成功rn else rn $this->error = $Upload->getError();rn return false;rn rn rn /**rn * 检测当前上传的文件是否已经存在rn * @param array $file 文件上传数组rn * @return boolean 文件信息, false - 不存在该文件rn */rn public function isFile($file)rn if(empty($file['md5']))rn throw new \Exception('缺少参数:md5');rn rn /* 查找文件 */rn $map = array('md5' => $file['md5'],'sha1'=>$file['sha1'],);rn return $this->field(true)->where($map)->find();rn rn /**rn * 清除数据库存在但本地不存在的数据rn * @param $datarn */rn public function removeTrash($data)rn $this->where(array('id'=>$data['id'],))->delete();rn rnrn[/code]rnWeiboModel.calss.phprn[code=php]rn public function addWeibo($uid, $content = '', $type = 'feed', $feed_data = array(), $from = '')rn rn //写入数据库rn $data = array('uid' => $uid, 'content' => $content, 'type' => $type, 'data' => serialize($feed_data), 'from' => $from);rn $data = $this->create($data);rn if (!$data) return false;rn $weibo_id = $this->add($data);rn //返回微博编号rn return $weibo_id;rn rn[/code]rn希望指教一下如何批量插入如表3,想了自己的几个办法都没有成功。。 论坛

没有更多推荐了,返回首页