前端代码
<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']));
}