tp5和layui的文件上传


 if (request()->isPost()) { $file = request()->file('img'); if ($file) { $info = $file->validate(['size' => 15678000, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads'); if ($info) { // echo $info->getSaveName();
                    $where['img'] = '/uploads//' . $info->getSaveName(); } else { echo $file->getError(); } } $in = db('tp')->insert($where); if ($in) { $this->success('新增成功!'); } }

tp5文件上传

php

    public function upload(){ $file = request()->file('file'); $info = $file->validate(['size' => 15678000, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads'); $arr = []; if($info){ $arr['code'] = 1; $arr['msg'] = '/uploads'.DS. $info->getSaveName(); return $arr; }else{ $arr['code'] = 2; $arr['msg'] = "上传失败"; return $arr; }; }

html

          <button type="button" class="layui-btn" id="test1">
            <i class="layui-icon">&#xe67c;</i>上传图片 </button>
          <img src="" alt="" id="a">
          <input type="hidden" id="b" name="img">



  <script> layui.use('upload', function () { var upload = layui.upload; var uploadInst = upload.render({ elem: '#test1' //绑定元素
 ,url: '{:url("index/upload")}' //上传接口
 ,done: function (res){ $('#a').attr('src', res.msg); $('#b').val(res.msg); } //上传完毕回调
 ,error: function () { // 
 } }); }); </script>

layui 文件上传


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM