文章编辑--包含文件上传与保存

文章编辑控制器代码

//渲染编辑文章的页面
    public function artEdit()
    {
        //1.获取到分类的id
        $artId = Request::param('id');
        //2.根据主键查询要更新的文章信息
        $artInfo = ArtModel::where('id',$artId)->find();

        //获取栏目信息
        $cateList = CateModel::all();
        //3.设置模板变量
        $this->view->assign('title','编辑文章');
        $this->view->assign('artInfo',$artInfo);
        $this->view->assign('cateList',$cateList);

        //渲染显示模板
        return $this->view->fetch('artedit');
    }
    //执行更新操作
    public function doEdit()
    {
        //1.获取用户提交的信息
        $data = Request::post();
        //2.获取上传图片信息
        //验证成功
        //获取一下上传图片的信息  也就是文件对象 title_img与前端字段相同且与数据库字段相同
        $file = Request::file('title_img');
        //文件信息验证成功后,再上传到服务器上的指定 目录,以 public 为起始目录
        $info = $file->validate([
            'size'=>1000000,
            'ext'=>'jpeg,jpg,png,gif',
        ])->move('uploads/');
        if($info){
            $data['title_img'] = $info->getSaveName();
        }else{
            $this->error($file->getError());
        }
        //将数据写到数据表中
        if(ArtModel::update($data)){
            $this->success("文章更新成功","artList");
        }else{
            $this->error("文章保存失败");
        }

    }

前端文章编辑页artedit.html

{layout name="public:layout" /}
<h4 class="text-center text-success">编辑文章</h4>
<form action="{:url('article/doEdit')}" method="post" enctype="multipart/form-data">
    <!--隐藏域携带 用户id-->
    <input type="hidden" name="id" value="{$artInfo.id}">
    <div class="form-group">
        <label for="title" class="col-sm-2 control-label">标题</label>
        <input type="text" name="title" class="form-control" id="title" value="{$artInfo.title}">
    </div>
    <div class="form-group">
        <label for="title" class="col-sm-2 control-label">分类</label>
        <select name="category_id" id="" class="form-control">
            {volist name="cateList" id="cate"}
            <option value="{$cate.id}">{$cate.name}</option>
            {/volist}
        </select>
    </div>
    <div class="form-group">
        <label for="title" class="col-sm-2 control-label">内容</label>
        <textarea class="form-control" name="content"  id="content" style="min-height: 250px">{$artInfo.content}
        </textarea>
    </div>
    <img src="/zh/public/uploads/{$artInfo.title_img}" class="img-rounded" width="100px">
    <div class="form-group">
        <label for="title" class="col-sm-2 control-label">标题图片</label>
        <input type="file" name="title_img" class="form-control" id="title_img" placeholder="标题图片">
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-primary" id="submit">保存</button>
    </div>
</form>

显示结果页
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值