TP插入数据的简单方法(包含上传图片,生成缩略图)


//添加数据页面
public function add(){
$this->display();
}
public function tij(){
//添加到goods表
$goods = M('goods');
//添加整条数据
$data = $goods->create();
if(IS_POST){
if($_FILES['goods_image']['error']==0){
$config = array(
//图片存储路径
'rootPath' => './Application/Admin/Pubilc/uploads/',
);
//添加原图路径到库
$upload = new ThinkUpload($config);
$info = $upload->uploadOne($_FILES['goods_image']);
$data['goods_image'] = $info['savepath'].$info['savename'];

//生成缩略图
$img=new ThinkImage();
//打开原图
$big_img = $upload->rootPath.$data['goods_image'];
// var_dump($data['goods_image']);
$img->open($big_img);
//缩略图尺寸   宽    高   
$img->thumb(100,150,2);
//保存
$small_img = $upload->rootPath.$info['savepath'].'small_'.$info['savename'];
$img->save($small_img);

$data['goods_image_ori']=$info['savepath'].'small_'.$info['savename'];
if($goods->add($data)){
//成功返回显示页
$this->redirect('lists',array(),0,'<script>alert("添加完成")</script>');
}else{
//失败跳回本页继续添加
$this->redirect('add',array(),0,'<script>alert("添加失败")</script>');
}
}
}else{
var_dump($upload->getErroe());
exit();
}
}

原文地址:https://www.cnblogs.com/sword082419/p/9276319.html