附件上传/缩略图

Posted on Posted in thinkphp
Tips: 本文创建于2014年5月16日,已超过 2 年,内容或图片可能已经失效!

相关文件

ThinkPHP/library/think/Upload.class.php

think/Image.class.php

  • <form enctype="multipart/form-data">
  • $_FILES接收附件信息
  • move_uploaded_file(); 把附件从临时路径名移动到指定目录

V模块

[code lang="html"]
<tr>
<td>商品图片</td>
<td><input type="file" name="goods_pic" /></td>
</tr>
[/code]

[code lang="html"]
<td><img src="{$smarty.const.__ROOT__}/{$v.goods_big_img}" height="60" width="40"/></td>
<td><img src="{$smarty.const.__ROOT__}/{$v.goods_small_img}" height="40" width="40"/></td>
[/code]

C模块

[code lang="php"]
public function add() {
$obj = D ( "Goods" );
if (! empty ( $_POST['goods_name'] )) {

if(!empty($_FILES['goods_pic']['name'])){
//收集附件信息,实现附件真正上传
//实例化Upload.class.php对象,对象调用相关方法实现附件上传
$cfg = array(
'rootPath' =>'./public/upload/',
);
$up = new \Think\Upload($cfg);
//public function uploadOne($file){
$rst = $up -> uploadOne($_FILES['goods_pic']);
//$rst返回值里边有附件上传后的对应信息
$picpathname = $up->rootPath.$rst['savepath'].$rst['savename'];
$_POST['goods_big_img'] = $picpathname;

//把上传的图片制作为缩略图Image.class.php
$im = new \Think\Image();
$im -> open($picpathname); //把原图给打开
$im -> thumb(150,150,2); //设置缩略图大小,默认为1
$smallpathname = $up->rootPath.$rst['savepath']."small_".$rst['savename'];
$im -> save($smallpathname); //把生成的缩略图存入系统指定目录
$_POST['goods_small_img'] = $smallpathname;
}

$obj->create ();
$res = $obj->add ();
if ($res) {
$this->success ( '添加商品成功', U ( 'Goods/show' ) );
} else {
$this->error ( '添加失败', U ( 'Goods/show' ) );
}
} else {
$this->display ();
}
}
[/code]

» 转载请注明来源:若我若鱼 » 附件上传/缩略图

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × 3 =