Thinkphp3.2.3 ----后台----图片上传相应处理
2017-03-03 16:31
183 查看
首先看看表结构
img_big 大图路径
img_small 小图路径
从 model 层入手,添加
在到 controller 层
添加完成,现到更新,model 入手层
在到 controller 层
还有 view 层(注意 if 的拼接)
注意:上传文件图片记得在 form 里增加此项(
)
如图
Field | Type | Comment | |
brandId | int(11) NOT NULL | ||
brandName | varchar(100) NOT NULL | ||
img_big | varchar(150) NOT NULL | ||
img_small | varbinary(150) NOT NULL | ||
brandDesc | text NULL | ||
createTime | datetime NOT NULL | ||
brandFlag | tinyint(4) NOT NULL |
img_small 小图路径
从 model 层入手,添加
//添加 public function added() { $brandInfo = $this->create(); if( !empty( $brandInfo ) ) { // 实例化上传类 $upload = new \Think\Upload(); // 设置附件上传大小(8) $upload->maxSize = 10240000; // 设置附件上传类型 $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传目录 $upload->rootPath = './Public/Uploads/'; // 设置附件上传(子)目录 $upload->savePath = ''; // 上传文件 $info = $upload->upload(); if( !empty( $info ) ) { //得到图片上传路径 $orImg = "./Public/Uploads/" . $info['img_big']['savepath'] . $info['img_big']['savename']; $image = new \Think\Image(); $image->open( $orImg ); //切割图片(文件名和后缀名) list( $fileName, $ext ) = explode( ".", $info['img_big']['savename'] ); $imgSmall = "./Public/Uploads/" . $info['img_big']['savepath'] . $fileName . '_s.' . $ext; // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg $image->thumb( 150, 150 )->save( $imgSmall ); //保存在数据库中的路径 $brandInfo['img_big'] = "./Public/Uploads/" . $info['img_big']['savepath'] . $info['img_big']['savename']; $brandInfo['img_small'] = "./Public/Uploads/" . $info['img_big']['savepath'] . $fileName . '_s.' . $ext; } $res = $this->add( $brandInfo ); if( $res !== false ) { return true; }else { return false; } }else { return false; } }
在到 controller 层
//添加 public function add() { $this->assign( "act", U( "insert" ) ); $this->assign( "actIn", "品牌添加" ); $this->assign( "actInfo", "添加" ); $this->display( "brand_info" ); } public function insert() { $res = D( "brand" )->added(); if( $res !== false ) { $this->success( "添加成功" ); }else { $this->error( "添加失败" ); } }
添加完成,现到更新,model 入手层
//更新 public function update() { $brandInfo = $this->create(); if( !empty( $brandInfo ) ) { // 实例化上传类 $upload = new \Think\Upload(); // 设置附件上传大小(8) $upload->maxSize = 10240000; // 设置附件上传类型 $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传目录 $upload->rootPath = './Public/Uploads/'; // 设置附件上传(子)目录 $upload->savePath = ''; // 上传文件 $info = $upload->upload(); if( !empty( $info ) ) { //得到图片上传路径 $orImg = "./Public/Uploads/" . $info['img_big']['savepath'] . $info['img_big']['savename']; $image = new \Think\Image(); $image->open( $orImg ); //切割图片(文件名和后缀名) list( $fileName, $ext ) = explode( ".", $info['img_big']['savename'] ); $imgSmall = "./Public/Uploads/" . $info['img_big']['savepath'] . $fileName . '_s.' . $ext; // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg $image->thumb( 150, 150 )->save( $imgSmall ); //保存在数据库中的路径 $brandInfo['img_big'] = "./Public/Uploads/" . $info['img_big']['savepath'] . $info['img_big']['savename']; $brandInfo['img_small'] = "./Public/Uploads/" . $info['img_big']['savepath'] . $fileName . '_s.' . $ext; } $res = $this->save( $brandInfo ); if( $res !== false ) { return true; }else { return false; } }else { return false; } }
在到 controller 层
//编辑 public function edit() { $this->assign( "brandInfo", D( 'brand' )->get() ); $this->assign( "act", U( "update" ) ); $this->assign( "actIn", "品牌更新" ); $this->assign( "actInfo", "更新" ); $this->display( "brand_info" ); } public function update() { $res = D( "brand" )->update(); if( $res !== false ) { $this->success( "更新成功" ); }else { $this->error( "更新失败" ); } }
还有 view 层(注意 if 的拼接)
<div class="form-group"> <label for="lastname" class="col-sm-2 control-label">已上传图标</label> <div class="col-sm-6"> <notempty name="brandInfo.img_small"> <if condition="( is_file( $brandInfo['img_small'] ) )"> <img src="__ROOT__/{$brandInfo.img_small}" /> </if> </notempty> </div> </div>
注意:上传文件图片记得在 form 里增加此项(
enctype="multipart/form-data"
)
如图
<form class="form-horizontal" role="form" method="post" action="{$act}" enctype="multipart/form-data">
相关文章推荐
- Thinkphp3.2.3 ----后台----图片上传相应处理
- Thinkphp3.2.3 ----后台----图片上传相应处理
- Thinkphp3.2.3 ----后台----图片上传相应处理
- Thinkphp3.2.3 ----后台----图片上传相应处理
- Thinkphp3.2.3 ----后台----图片上传相应处理
- Thinkphp3.2.3 ----后台----图片上传相应处理
- Thinkphp3.2.3 ----后台----图片上传相应处理
- Thinkphp3.2.3 ----后台----图片上传相应处理
- ThinkPHP 3.2.3图片处理
- KindEditor的使用和上传图片的后台处理
- KindEditor的使用和上传图片的后台处理
- js上传图片包括后台处理
- 后台处理批量上传图片的方法
- KindEditor的使用和上传图片的后台处理
- thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印
- 使用ThinkPHP上传类处理KindEditor上传的图片(主要是注意返回格式的方式)
- 关于Java后台处理android上传图片的问题
- 上传图片+浏览+裁切 Demo(无后台处理部分)
- thinkphp3.2.3 ueditor1.4.3 图片上传操作,在线删除上传图片功能。