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"></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 文件上傳