本文来自文库分享网www.wkfxw.com
本示例所使用的是thinkphp系统自带的类。UploadFile.calss.php。完成商品的添加,商品图片的上传。
具体示例代码如下:
GoodsAction.class.php
<?php
class GoodsAction extends CommonAction{
public function doAdd(){
layout(false);
$data=$this->getGoodsData();
$pic=$_FILES['filename']['pic'];
import('ORG.Net.UploadFile'); //引入thinkphp系统自带的上传类文件。
$upload=new UploadFile(); //实例化。
$upload->maxSize=102400000; //文件上传大小。
$upload->allowExt=array('jpg','png','gif');//允许上传文件的类型。
$upload->savePath='./Public/uploads/'; //文件上传保存路路径。
if(!$upload->upload()){
$upload->getErrorMsg();
}else{
$pic=$upload->getUploadFileInfo();
}
$data['pic']=$pic[0]['savename'];
if(M('goods')->add($data)){
$this->success('成功添加商品',U('lists'));
}else{
$this->error('添加商品失败');
}
}
public function getGoodsData(){
$data['goods_name']=I('post.goods_name');
$data['price']=I('post.price');
$data['category_id']=I('post.category_id');
$data['brand_id']=I('post.brand_id');
$data['storage']=I('post.storage');
$data['sendTime']=time();
return $data;
}
}
?>
商品添加视图部分
<div class="userList">
<div class="siteCon clear">
<p class="wenzhangtishi">添加商品</p>
</div>
<div style="padding-left:10px;">
<button><a href="__URL__/lists">商品列表</a></button>
<button><a href="__URL__/add">添加商品</a></button>
</div>
<div style="margin-top:10px;">
<form name="myform" method="POST" action="__URL__/doAdd" enctype="multipart/form-data">
<table width="80%" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="45" colspan="2" align="center">添加商品</td>
</tr>
<tr>
<td width="233" height="45" align="right">商品名称:</td>
<td width="530" height="45"><input type="text" name="goods_name" value="" /></td>
</tr>
<tr>
<td height="45" align="right">所属分类:</td>
<td height="45"><select name="category_id">
<option value="0">请选择商品分类</option>
{$category}
</select></td>
</tr>
<tr>
<td height="45" align="right">商品品牌:</td>
<td height="45"><select name="brand_id">
<option value="0">请选择商品牌</option>
{$brand}
</select> </td>
</tr>
<tr>
<td height="45" align="right"> 商品价格:</td>
<td height="45"><input type="text" name="price" value="" /></td>
</tr>
<tr>
<td height="45" align="right">商品图片:</td>
<td height="45"><input type="file" name="pic" /></td>
</tr>
<tr>
<td height="45" align="right">商品库存:</td>
<td height="45"><input type="text" name="storage" value="" /></td>
</tr>
<tr>
<td height="45" colspan="2" align="center"><input type="submit" value="添加商品" /></td>
</tr>
</table>
</form>
</div>