您的位置:首页 > 编程语言 > PHP开发

Thinkphp3.2.3 ----后台----图片上传相应处理

2017-03-03 16:31 183 查看
首先看看表结构

 FieldTypeComment
brandIdint(11) NOT NULL 
 brandNamevarchar(100) NOT NULL 
 img_bigvarchar(150) NOT NULL 
 img_smallvarbinary(150) NOT NULL 
 brandDesctext NULL 
 createTimedatetime NOT NULL 
 brandFlagtinyint(4) NOT NULL
img_big 大图路径

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">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: