php layui 上传文件并把路径添加数据库依托帝国cms系统二开

html文件

<html>
<head>
  <meta charset="utf-8">
  <title>添加轮播图</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="skin/layui/css/layui.css" media="all">
</head>
<body>

 <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
  <legend>添加轮播图</legend>
</fieldset>
<form class="layui-form layui-form-pane" style="padding:0 20px" action="addpic.php" method="post">

  <div class="layui-form-item">
    <label class="layui-form-label">名称</label>
    <div class="layui-input-inline">
      <input type="text" name="name" lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
    </div>
  </div>
  
    <div class="layui-form-item">
    <label class="layui-form-label">缩略图</label>
    <div class="layui-input-block">
      <div class="layui-upload">
          <button type="button" class="layui-btn" id="upimg">上传图片</button>
          <div class="layui-upload-list">
            <img class="layui-upload-img" style="width:200px;" id="imgpreview"> <!--这里的id里面的值要和后面js中的一致,不然无法回显-->
            <p id="demoText"></p>
            <input type="text" name="burl" id="imgsrc" class="layui-input" style="display: none;">
          </div>
        </div>  
    </div>

  
  <div class="layui-form-item">
    <label class="layui-form-label">顺序</label>
    <div class="layui-input-inline">
      <input type="text" name="myoder" lay-verify="required" placeholder="请输入自定义顺序" autocomplete="off" class="layui-input">
    </div>
  </div>

  <div class="layui-form-item" style="text-align: center;">
    <button class="layui-btn" lay-filter="demo2">立即添加</button>
  </div>
</form>
<script src="skin/layui/layui.js"></script>  
<script>
//上传功能
layui.use('upload', function(){
  var $ = layui.jquery
  ,upload = layui.upload;
  
  //普通图片上传
  var uploadInst = upload.render({
    elem: '#upimg'
    ,url: 'addpic.php'
    ,accept:'images'
    ,field:'file'
    ,method:'get'
    ,done: function(res){
        var imgsrc=res.imgsrc;
        var thumbsrc=res.thumbsrc;
        document.getElementById("imgsrc").value = imgsrc;
        document.getElementById("imgpreview").src = thumbsrc;
        return layer.msg('上传成功');
      //如果上传失败
      if(res.code > 0){
        return layer.msg('上传失败');
      }
      //上传成功
    }
    ,error: function(){
      //演示失败状态,并实现重传
      var demoText = $('#demoText');
      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
      demoText.find('.demo-reload').on('click', function(){
        uploadInst.upload();
      });
    }
  });
});

</script>

</body>
</html>

php文件

<?php
define('EmpireCMSAdmin','1');
require("../../class/connect.php");
require("../../class/db_sql.php");
require("../../class/functions.php");
require("../../class/t_functions.php");
header("Content-Type: text/html;charset=utf-8");
$link=db_connect();
$empire=new mysqlquery();
$editor=1; //声明目录层次

$imgurl="https://访问域名/e/extend/xcx_link/upload";
// 允许上传的图片后缀
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
// echo $_FILES["file"]["size"];
$extension = end($temp);     // 获取文件后缀名
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20480000)   // 小于 20M,这个自己限制
&& in_array($extension, $allowedExts))
{
    
    $imgpath=$_GET['imgpath'];  //获取传来的图片分类,用于在服务器上分类存放
    $code = $_FILES['file'];//获取小程序传来的图片 
    $uploaded_file=$_FILES['file']['tmp_name'];  
    $user_path="upload/".date('Ymd');  //放到服务器下指定的文件夹
  if(file_exists($user_path)){
  }else{
  mkdir($user_path,0777); 
  }
    $size=$_FILES["file"]["size"];
    
    $date=date('YmdHis');        //得到当前时间
    $newfilename=$date.rand(1000,9999).'.'.$extension;        //得到一个新的文件名,可根据自己需求设定,sham用的时间加上图片文件大小来命名
    $move_to_file=$user_path."/".$newfilename;  
    
    $file_true_name=$imgurl.$date."/".$newfilename; 
    //echo $file_true_name;
    $filename = json_encode($file_true_name);//把数据转换为JSON数据.
   // echo $move_to_file;
    move_uploaded_file($uploaded_file,iconv("utf-8","gb2312",$move_to_file));
 
 //下面的代码是用来生成缩略图的 
  $thump = $user_path."/".date('Ymd')."/thumb/";   //这个缩略图文件夹地址自己设置,这个是在原图文件夹里面增加1个子目录thumb用于存放缩略图
  if(file_exists($thump)){
  }else{
  mkdir($thump,0777); 
  }
  $imgs = $newfilename;
  $imgss=$user_path."/".$imgs;
  $img_arr = getimagesize($imgss); 
  $pecent = $img_arr[0]/$img_arr[1];
  $width = 200;    //这里是缩略图的尺寸,自行设置
  $height = 200/$pecent; 
//下面是根据不同图片后缀,执行不同的图片生成代码
  if($_FILES["file"]["type"] == "image/png"){
    $img_in = imagecreatefrompng($imgss);
  }elseif($_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/pjpeg"){
    $img_in = imagecreatefromjpeg($imgss);
  }elseif($_FILES["file"]["type"] == "image/gif"){
    $img_in = imagecreatefromgif($imgss);
  }
  
  $img_out = imagecreatetruecolor($width, $height); 
  imagecopyresampled($img_out, $img_in, 0, 0, 0, 0, $width, $height, $img_arr[0],             
  $img_arr[1]); 
  imagejpeg($img_out,$thump.$imgs,100); 
  imagedestroy($img_out);
  imagedestroy($img_in);
  
 
  $imgsrc=$imgurl.$imgpath."/".date('Ymd')."/".$newfilename;
 
  $thumbsrc=$imgurl.$imgpath."/".date('Ymd')."/".$newfilename;
 
  $json = json_encode($imgsrc);//把数据转换为JSON数据.
 
  
 
  echo "{".'"code": 0,'.  '"msg": "done",'.'"imgsrc": "'.$imgsrc.'",'.'"thumbsrc": "'.$thumbsrc.'",'. '"data"'.":".$json."}";
 
  //这里一定要有code,msg,$json,不然好像就会报错,就是这个折腾了我好久,新手之郁闷

    



 
}else{
    
    if($_POST['name']==''||$_POST['burl']==''){}else {
      $keyadd=$empire->query("INSERT INTO `phome_ecms_xcx_banner` (`name`,`burl`,`myoder`,`btime`) VALUES('".$_POST['name']."', '".$_POST['burl']."', '".$_POST['myoder']."', '".date("Y-m-d H:i:s")."')");
    }
    
    echo "<script>window.alert('添加轮播图成功!');window.location.href='addbanner.html';</script>";//返回原来页面
}   


  

db_close(); //关闭MYSQL链接
$empire=null; //注消操作类变量
?>

mysql信息
 

 if($_POST['name']==''||$_POST['burl']==''){}else {
      $keyadd=$empire->query("INSERT INTO `phome_ecms_xcx_banner` (`name`,`burl`,`myoder`,`btime`) VALUES('".$_POST['name']."', '".$_POST['burl']."', '".$_POST['myoder']."', '".date("Y-m-d H:i:s")."')");
    }