thinkphp5以及Layui结合如何实现图片上传并预览(代码)

不言
不言 原创
2023-04-03 19:36:01 3103浏览

本篇文章给大家带来的内容是关于thinkphp5以及Layui结合如何实现图片上传并预览(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

html代码

<div class="layui-upload">
   <button type="button" class="layui-btn" id="cover">上传封面</button></div> <div class="layui-input-inline">
   <img id="preview" width="200px" height="200px"></div>

js代码

var uploadInst = upload.render({
      elem:'#cover'
      ,url:'addCourse'
      ,accept:'file'  // 允许上传的文件类型
      ,auto:true // 自动上传
      ,before:function (obj) {
          console.log(obj);
          // 预览
          obj.preview(function(index,file,result) {
              // console.log(file.name);    //图片名字
              // console.log(file.type);    //图片格式
              // console.log(file.size);    //图片大小
              // console.log(result);    //图片地址
              $('#preview').attr('src',result); //图片链接 base64
          });
          // layer.load();
      }
      // 上传成功回调
      ,done:function(res) {
          // console.log(upload);
          console.log(res);
          
      }
      // 上传失败回调
      ,error:function(index,upload) {
          // 上传失败
      }
      
  });

php接口

$file = request()->file('file');
 // 移动到框架应用根目录/public/uploads/ 目录下
 $info = $file->move('public/upload/');
 if ($info) {
     $path = 'public/upload/'.$info->getSaveName();
     return return_succ($path);
 }

相关推荐:

PHP——图片上传 html上传图片代码 图片上传插件 php php上传图片代

PHP+ajax图片上传的简单实现 mvc ajax 上传图片 jquery ajax 上传图片 php ajax 上传图

以上就是thinkphp5以及Layui结合如何实现图片上传并预览(代码)的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。