文件上传
用到的知识:
1.文件上传的时候要设置表单的enctype属性 如下:
'2\$_FILE[名字][]用来接收文件的信息
第二维的字段
name
size
error
type
tmp_name
3、move_uploaded_file (临时文件,目标文件)
tp自带了上传文件的类,位置:Think/Upload.class.php
$goods = M('goods'); if(IS_POST){ if($data = $goods->create()){ if($_FILES['goods_img']['error'] == 0){ $config = array( 'rootPath' => './Application/Uploads/', //保存根路径 ); $upload = new \Think\Upload($config); $info = $upload->uploadOne($_FILES['goods_img']);// 上传的方法 只上传一张 返回的是上传的文件的额信息 $data['goods_big_img'] = $info['savepath'].$info['savename']; if($goods->add($data)){ $this->success('tianjiacehgngong','showlist',3); }else{ $this->error('tianjaishibai'); } }else{ var_dump($upload->getError); } } }
生成缩略图
缩略图类存放的位置 Think\Image.class.php
if(IS_POST){ if($data = $goods->create()){ if($_FILES['goods_img']['error'] == 0){ $config = array( 'rootPath' => './Application/Uploads/', //保存根路径 ); $upload = new \Think\Upload($config); $info = $upload->uploadOne($_FILES['goods_img']);// 上传的方法 只上传一张 返回的是上传的文件的额信息 $data['goods_big_img'] = $info['savepath'].$info['savename']; //生成缩略图 $img = new \Think\Image(); //1.打开图片 $big_img = $upload->rootPath.$data['goods_big_img']; //获得大图片的绝对路径 $img->open($big_img); //打开大图片 //2、生成缩略图 $img->thumb(200,300,2); //设定宽高和怎样缩略 //3、保存 $small_img = $upload->rootPath.$info['savepath'].'small'.$info['savename']; $img->save($small_img); $data['goods_small_img'] = $small_img; if($goods->add($data)){ $this->success('tianjiacehgngong','showlist',3); }else{ $this->error('tianjaishibai'); } }else{ var_dump($upload->getError); } }