<?php $arr=[]; if ($_FILES) { if ($_FILES[‘file‘][‘type‘] == ‘file/png‘ || $_FILES[‘file‘][‘type‘] == ‘file/jpg‘ || $_FILES[‘file‘][‘type‘] == ‘file/jpeg‘ || $_FILES[‘file‘][‘type‘] == ‘file/gif‘ || $_FILES[‘file‘][‘size‘] < 1 * 1024 * 1024) { if ($_FILES[‘file‘][‘error‘]) { echo ($_FILES[‘file‘][‘error‘]); $arr[‘code‘]=2; } else { $arr = explode(‘.‘, $_FILES[‘file‘][‘name‘]); $type = end($arr); $thumb = "/upload/pd" . time() . rand(1000, 9999) . ‘.‘ . $type; move_uploaded_file($_FILES[‘file‘][‘tmp_name‘], "." . $thumb); $arr[‘code‘]=1; $arr[‘msg‘]=$thumb; } }else{ $arr[‘code‘]=3; } } echo json_encode($arr); ?>
//layui var uploadinst = upload.render({ elem: ‘#test1‘, url: ‘/upload.php‘, done: function(res) { $(‘#demo1‘).attr(‘src‘, res.msg); $(‘#thumb‘).val(res.msg); } });
<div class="layui-upload"> <button type="button" class="layui-btn" id="test1">上传图片</button> <div class="layui-input-block" style="margin-top:5px;"> <img class="layui-upload-img" id="demo1" src=""> <input type="hidden" name="thumb" id="thumb" value="res"> </div> </div>