- 将图片预览插件uploadPreview.js下载好放入Common->Js中
- 模板中导入js文件,及主要代码
<script type="text/javascript" src="{$Think.config.COMMON_URL}Js/uploadPreview.js"></script>
<form action="/subject/drgyhxtx.html" method="post" enctype="multipart/form-data">
<table border="1" width="100%" class="table_a" id="general-tab-tb">
<tr>
<td>头像</td>
<td>
<div id="user_logo_dv">
<img src="" alt="" id="user_logo_im" width="100" height="100"/>
</div>
<input type="file" name="user_logo" id="user_logo"/>
</td>
</tr>
</table>
<script type="text/javascript">
$(function(){
new uploadPreview({ UpBtn: "user_logo", DivShow: "user_logo_dv", ImgShow: "user_logo_im" });
});
</script>
</form>
- UserController.class.php相关代码如下
//添加会员
public function add(){
$user=new \Model\UserModel();
if(IS_POST){
$data=$user->create();
if($data){
if($user->add($data)){
$this->success('添加会员成功!',U('showlist'),2);
}else{
$this->error('添加会员失败!',U('add'),2);
}
}else{
$this->assign('info',$user->getError());
$this->display();
}
}else{
$this->display();
}
}
- UserModel.class.php文件相关代码如下:
// 插入数据前的回调方法
protected function _before_insert(&$data,$options) {
//上传图片处理
if($_FILES['user_logo']['error']===0){
//A.上传原图图片
//通过Think/Upload.class.php实现附件上传
$cfg=array(
'rootPath'=>'Common/Uploads/',
);
$up=new \Think\Upload($cfg);
$z=$up->uploadOne($_FILES['user_logo']);
//拼接图片路径名信息
$big_path_name=$up->rootPath.$z['savepath'].$z['savename'];
$data['user_logo']=$big_path_name;
}
}
网友评论