THINKPHP 模板上传图片--后台接收图片

模板

{extend name="public/base" /}
{block name="body"}


    <div class="row">
        <div class="col-md-8">

            <div class="page-header">
              <h2>发布文章</h2>
            </div>
            <form id="uploadForm">
<input type="hidden" value="{$Think.session.user_id}" name="user_id" id="user_id" />

              <div class="form-group">
                <label>标题</label>
                  <input type="text" name="title" id="title" class="form-control" placeholder="文章标题">
              </div>


              <div class="form-group">
                <label >栏目</label>
                    <select class="form-control" name="cate_id" id="cate_id">
                    {volist name="cateList" id="vo"}
                        <option value="{$vo.id}">{$vo.name}</option>
                    {/volist}
                    </select>
              </div>

              <div class="form-group">
                <label>内容</label>
                    <textarea id="content" class="form-control" rows="8" name="content" placeholder="内容"></textarea>
              </div>

            
              <div class="form-group">
                <label >封面图</label>
                    <input type="file" name="title_img" id="inputfile" >
                    <!-- <p class="help-block">Example block-level help text here.</p> -->
                    <div id="info" style=" color:red;margin-bottom: 3px;margin-top: 8px;"></div>
              </div>


            </form>

               <div class="form-group">
                  <button  class="btn btn-primary" id="register" style="width:100%">发布</button>
              </div>



        </div>

        <script>
            $(function(){

                $("#register").on('click',function(){

                    var img = $("#inputfile").prop("files")[0];
                    var formData = new FormData();
                    formData.append('title_img', img);
                    formData.append('user_id', $('#user_id').val());
                    formData.append('title', $('#title').val());
                    formData.append('cate_id',$('#cate_id').val());
                    formData.append('content', $('#content').val());


                    $.ajax({
                        url:"{:url('index/insertcl')}",
                        type:"post",
                        data:formData,
                        dataType:'json',
                        cache: false,
                        processData: false,
                        contentType: false,                    
                        success:function(data){
                            //如果输入不合格
                            if(data.status==-1){
                            $('#info').text(data.message);
                            }else{
                                alert(data.message);
                                window.location.href="{:url('index/index')}";
                            }
                        }
                    });
            });
        });
        </script>
{/block}

 

 

后台

    //发布文章操作
    public function insertcl(){
        $data = $_POST;
        $data['title_img'] =request()->file('title_img');
        //验证器
        $rule = 'app\common\validate\Article';
        $res = $this->validate($data,$rule);
        //验证结果
        if($res!==true){
            return ['status'=>-1,'message'=>$res];
        }    
        
        //获取file对象
        $file =  $data['title_img']; 
        //文件信息验证与上传到服务器指定目录
        $info = $file->validate([
            'size'=>5000000,  //文件大小
            'ext'=>'jpg,jpeg,png,gif'  //文件拓展名
        ])->move('uploads/');  //移动到public/uploads目录下

        //错误就报错
        if($info==false){
            return ['status'=>-1,'message'=>$file->getError()];
        }

        //获取文件名
        $data['title_img'] = $info->getSaveName();

        $result = Article::create($data);

        if($result){
            return ['status'=>1,'message'=>'发布成功'];
        }
        return ['status'=>0,'message'=>'发布失败'];


    }

 

posted @ 2018-08-24 13:05  cl94  阅读(419)  评论(0编辑  收藏  举报