Many forms on the website will be used to upload pictures, logos, photos, and users will also upload pictures. At this time, the website needs a function to upload pictures, and after uploading, I hope to preview it to see if it is uploaded correctly.
thinkphp5 adds layui to implement the image upload function (with image preview), asynchronously transmit the image and preview it, return the asynchronously uploaded value to the hidden field of the form and then submit it.
First of all, it is necessary to introduce the jQuery file
<link rel="stylesheet" type="text/css" href="__STATIC__/plugins/layui/css/layui.css" rel="external nofollow" > <script type="text/javascript" src="__STATIC__/plugins/layui/layui.js"></script>
<form class="layui-form"> <div class="layui-input-inline"> <button class="layui-btn layui-btn-sm" onclick="return false;" id="upload_img"><i class="layui-icon"></i>上传图片</button> <img id="pre_img" <?php if($data['item']['img']){echo 'src="'.$data['item']['img'].'"';}?> /> <input type="hidden" name="img" value="{$data.item.img}"> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button class="layui-btn" onclick="save()">保存</button> </div> </div> </form>
<script type="text/javascript"> layui.use(['form','layer','upload'],function(){ $ = layui.jquery; var form = layui.form; layer = layui.layer; var upload = layui.upload; //执行实例 var uploadInst = upload.render({ elem: '#upload_img' //绑定元素 ,url: '/admins.php/admins/video/upload_img' //上传接口 ,accept:'images' ,done: function(res){ //上传完毕回调 $('#pre_img').attr('src',res.msg); $('input[name="img"]').val(res.msg); } ,error: function(){ //请求异常回调 } }); }); // 保存 function save(){ var title = $.trim($('input[name="title"]').val()); var url = $.trim($('input[name="url"]').val()); if(title == ''){ layer.msg('请输入影片名称',{'icon':2,'anim':6}); return; } if(url == ''){ layer.msg('请输入影片地址',{'icon':2,'anim':6}); return; } $.post('/admins.php/admins/video/save',$('form').serialize(),function(res){ if(res.code>0){ layer.msg(res.msg,{'icon':2,'anim':2}); }else{ layer.msg(res.msg,{'icon':1}); setTimeout(function(){parent.window.location.reload();},1000); } },'json'); } </script>
public function upload_img(){ $file = request()->file('file'); if($file==null){ exit(json_encode(array('code'=>1,'msg'=>'没有文件上传'))); } $info = $file->move(ROOT_PATH.'public'.DS.'uploads'); $ext = ($info->getExtension()); if(!in_array($ext,array('jpg','jpeg','gif','png'))){ exit(json_encode(array('code'=>1,'msg'=>'文件格式不支持'))); } $img = '/uploads/'.$info->getSaveName(); exit(json_encode(array('code'=>0,'msg'=>$img))); }
public function save(){ $id = (int)input('post.id'); $data['title'] = trim(input('post.title')); $data['channel_id'] = (int)input('post.channel_id'); $data['charge_id'] = (int)input('post.charge_id'); $data['area_id'] = (int)input('post.area_id'); $data['img'] = trim(input('post.img')); $data['url'] = trim(input('post.url')); $data['keywords'] = trim(input('post.keywords')); $data['desc'] = trim(input('post.desc')); $data['status'] = (int)input('post.status'); if($data['title'] == ''){ exit(json_encode(array('code'=>1,'msg'=>'影片名称不能为空'))); } if($data['url'] == ''){ exit(json_encode(array('code'=>1,'msg'=>'影片地址不能为空'))); } if($id){ $this->db->table('video')->where(array('id'=>$id))->update($data); }else{ $data['add_time'] = time(); $this->db->table('video')->insert($data); } exit(json_encode(array('code'=>0,'msg'=>'保存成功'))); }
The above is the detailed content of How to add layui to thinkphp5 to implement image upload function. For more information, please follow other related articles on the PHP Chinese website!