html
<form action="admin/create_book.php" enctype="multipart/form-data">
<input type="file" name="file_img" accept="image/*">
</form>
js
$(document).ready(function(){
$.ajax({
url: "xxx.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
})
.done(function(data, textStatus, jqXHR) {//新增商品回傳到HTML成功的訊息
console.log(data.name);
});
});
PHP
$sourcePath = $FILES['fileimg']['tmpname'];
$targetPath1 = "../createimg/".$FILES['fileimg']['name'];//要上傳的路徑
if(moveuploadedfile($sourcePath,$targetPath1)){//照片上傳成功的時候
echo jsonencode(['上傳成功'。]);
}else{
echo jsonencode(['上傳失敗'。]);
}