---------------添加产品layui图片上传------
1.表单页面
这是把内容上传到数据库
<?php
if($_POST){
$title = $_POST['title'];
$intro = $_POST['desc'];
$content = $_POST['content'];
$uname = $_POST['img'];
// if($_FILES['file']['error']<=0){
// // var_dump($_FILES);
// $temp = explode(".",$_FILES['file']['name']);
// $extension = end($temp);
// $uname = '/upload/'.mt_rand(0,99).'.'.$extension;
// move_uploaded_file($_FILES['file']['tmp_name'],'../..'.$uname);
// }
$conn = new mysqli('localhost','root','root','denglu');
$sql = "insert into product(title,img,intro,content) values(' $title','$uname','$intro','$content')";
$red = $conn->query($sql);
$conn->close();
}
?>
1.1写在HTML页面里的
这是上传按钮是layui里面的
<button type="button" class="layui-btn" id="test1">
<i class="layui-icon"></i>上传图片
</button>
<img src="" alt="" id="imgs">
这是隐藏域
<input type="hidden" name="img" id="imgg" value="">
<script>
//执行实例
var uploadInst = upload.render({
elem: '#test1' //绑定元素
,url: '../upload.php' //上传接口
,field:'file'
,done: function(res){
//上传完毕回调
document.getElementById('imgs').src = res.msg;
document.getElementById('imgg').value = res.msg;
}
,error: function(){
//请求异常回调
}
});
});
</script>
2.这是写在upload.php这里面写的是修改图片路径 然后传给首页的js里面
<?php
if($_FILES){
// var_dump($_FILES);
$temp = explode(".",$_FILES['file']['name']);
$extension = end($temp);
$uname = '/upload/'.mt_rand(0,99).'.'.$extension;
move_uploaded_file($_FILES['file']['tmp_name'],'..'.$uname);
$data['code'] = 1;
$data['msg'] = $uname;
echo json_encode($data);
}
?>