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