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

浙公网安备 33010602011771号