thinkphp对上传文件

html

前台html文件 进行数据封装 from表单的参数enctype="multipart/form-data"

 <form action="{:U('/News/save_blog')}" method="post" id="myform" name="myform" enctype="multipart/form-data">
<td>
<input type="file" name="file" style="width: 80%; height: 30px" />
<i class="msg require-red"></i>
</td>
 public function save_blog(){


        if (is_uploaded_file ( $_FILES ['file'] ['tmp_name'] )) {
            $icon = $_FILES ['file'];

            $iconPath = "Public/images/news/" . time () . $icon ['name'];
            $data ["file"] ="/$iconPath";

            $icon ['name'] = iconv('utf-8','gb2312',$icon ['name']);
            $iconPath = "Public/images/news/" .time (). $icon ['name'];

            move_uploaded_file ( $icon ['tmp_name'], $iconPath );

        }

$icon ['name'] = iconv('utf-8','gb2312',$icon ['name']);
是为了解决文件上传的时候中文乱码的情况
然后然后才能给予正确的移动路径,然后再进行

$iconPath = "Public/images/news/" .time (). $icon ['name'];
move_uploaded_file ( $icon ['tmp_name'], $iconPath );

进行判读是增加文件还是编辑文件

        $id=I('post.id');
        if($id>0){

            $map['id']=$id;
            $data['add_time']=time();
            $res=$this->blog_model->where($map)->save($data);
            if($res){
                $this->success("编辑成功",U('/News/index'));
            }else{
                $this->success("编辑失败",U('/News/index'));
            }

        }else{

            $res=$this->blog_model->add($data);
            if($res){
                $this->success("添加成功",U('/News/index'));
            }else{
                $this->success("添加失败",U('/News/index'));
            }
        }
    }
posted @ 2018-02-01 08:26  孙中明  阅读(136)  评论(0编辑  收藏  举报