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

首先看看表结构

  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_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">

你可能感兴趣的:(Thinkphp)