html中

< form action= " << $smarty.const.__SELF__>>" method= "post" enctype= "multipart/form-data" >
< table border= "1" width= "100%" class= "table_a" >
< tr >
< td >商品名称 </ td >
< td >< input type= "text" name= "gname" /></ td >
</ tr >
< tr >
< td >商品价格 </ td >
< td >< input type= "text" name= "gprice" /></ td >
</ tr >
< tr >
< td >商品图片 </ td >
< td >< input type= "file" name= "gpic" /></ td >
</ tr >
< tr >
< td colspan= "2" align= "center" >
< input type= "submit" value= "添加" >
</ td >
</ tr >
</ table >
</ form >


controller中

     function addlist(){ //收集数据 展示表单两个逻辑
        $goods=D( 'Goods');
         if(!empty($_POST)){
             //处理上传的图片附件
             //dump($_FILES);
             if($_FILES[ 'gpic'][ 'error']< 4){
                $cfg=array(
                     'rootPath' => './Public/uploads/' //保存根路径
                );
                $arg= new \Think\Upload($cfg);
                $z=$arg->uploadOne($_FILES[ 'gpic']); //执行成功后会把附件名字,路径等信息返回
                 //dump($z);
                 //dump($arg->getError());
                 //数据库中存储路径名称
                $_POST[ 'gbpic']=$arg->rootPath.$z[ 'savepath'].$z[ 'savename'];

            }
            $shuju=$goods->create();
            $z=$goods->add($shuju);
             if($z){
                 $this->redirect( 'showlist', null, 3, '添加商品成功');
            } else{
                 $this->redirect( 'addlist',array(), 3, '添加商品失败');
            }
        } else{
             $this->display();
        }