js php 点击按钮上传图片

前端代码

<button class="add-img-btn"></button>

<script>

$(".add-img-btn").click(function () {
var thisObj = $(this);
var inputFile = document.createElement("input");
inputFile.type = "file";
inputFile.name = "file[]";
inputFile.multiple = "multiple";
inputFile.accept = "image/jpeg,image/jpg,image/png";
inputFile.click();

$(inputFile).change(function(){
var files = inputFile.files;
for(var i=0;i<files.length;i++){
uploadImg(files[i],thisObj);
}
});
});

function uploadImg(file,thisObj){
var formData = new FormData();
formData.append('image', file);
$.ajax({
url: "/api.php?c=common&a=uploadImg",
type: "post",
data: formData,
dataType:"json",
contentType: false,
processData: false,
mimeType: "multipart/form-data",
success: function (data) {
if(data.code == 0){
  console.log(data.path);
       }
},
});
}

</script>

 

php代码

public function uploadImg() {
$floder = "customer/".date("YmdHis");
@mkdir('image/'.$floder,0777);
@move_uploaded_file($_FILES['image']['tmp_name'],'../image/'.$floder."/".$_FILES['image']['name']);
echo json_encode(array("code"=>0,"path"=>"image/".$floder."/".$_FILES['image']['name']));
}
posted @ 2019-02-20 13:43  燃烧的腿毛  阅读(292)  评论(0编辑  收藏  举报