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'));
            }
        }
    }| 🐳  作者:hiszm 📢 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,万分感谢。 💬 留言:同时 , 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |  | 

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号