//更新 更新中的话,一定要写那一个if判断 public function update(){ $model = M(‘link‘); $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = 3145728 ;// 设置附件上传大小 $upload->exts = array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘);// 设置附件上传类型 $upload->rootPath = ‘./Public‘; //根下面的public $upload->savePath = ‘/uploads/‘; // 设置附件上传目录 $upload->saveName = [‘uniqid‘,‘pic‘];//设置保存前缀 $upload->subName = [‘date‘,‘Y/m/d‘]; //以日期方式保存 //上传文件 并 返回信息 $info = $upload->upload(); if($info){ //更新的话,一定要写这个判断 //遍历信息 取得路径和文件名 foreach($info as $file){ $img = ‘Public‘.$file[‘savepath‘].$file[‘savename‘]; } //把文件名存到POST中 方便create使用 $_POST[‘picname‘] = $img; } if($model->create()){ $model->save(); $this->success(‘更新成功‘,U("Link/select")); }else{ $this->error(‘更新失败‘,U("Link/select")); } }
2.添加。。。。
//保存添加的内容 public function insert(){ $model = M(‘link‘); $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = 3145728 ;// 设置附件上传大小 $upload->exts = array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘);// 设置附件上传类型 $upload->rootPath = ‘./Public‘; //根 $upload->savePath = ‘/uploads/‘; // 设置附件上传目录 $upload->subName = [‘date‘,‘Y/m/d‘]; //以日期方式创建文件夹 保存图片 $upload->saveName = [‘uniqid‘,‘pic‘];//设置保存前缀 $info = $upload->upload(); // 上传文件 // array (size=1) // ‘picname‘ => // array (size=9) // ‘name‘ => string ‘Meinv002.jpg‘ (length=12) // ‘type‘ => string ‘image/jpeg‘ (length=10) // ‘size‘ => int 557301 // ‘key‘ => string ‘picname‘ (length=7) // ‘ext‘ => string ‘jpg‘ (length=3) // ‘md5‘ => string ‘6807fdb5a7d40d72374f0049c46fb70d‘ (length=32) // ‘sha1‘ => string ‘8f9b545211b2dfa49957cf3e0e8f5e8cd68a734e‘ (length=40) // ‘savename‘ => string ‘pic55c74c898d07d.jpg‘ (length=20) // ‘savepath‘ => string ‘/uploads/2015/08/09/‘ (length=20 // dump($upload); // echo ‘<hr />‘; // dump($info); // die(); foreach($info as $file){ $img = ‘Public‘.$file[‘savepath‘].$file[‘savename‘]; } $_POST[‘picname‘] = $img; //名字传给他 if($model->create()){ $model->addtime = time(); $model->add(); $this->success(‘添加成功‘,U("Link/select")); }else{ $this->error(‘提交失败‘,U("Link/select")); } }
三、表单中
<form role="form" action=‘__URL__/insert‘ method=‘POST‘ enctype="multipart/form-data">
<div class="form-group"> <label>添加图片</label> <input type="file" name="picname"> </div>
时间: 2024-10-11 09:03:44