我現在有一個麻煩,插入一個文件輸入(圖像)到我的數據庫,但它會返回我空值,當我試圖將其插入數據庫。如何在php中顯示甜蜜警報後使用ajax上傳文件?
這是我的腳本
<script>
$("#insertform").on('submit',(function(e) {
/* $('#submit').toggleClass('active');
document.getElementById('submit').disabled = 'disabled'; */
var data=$('#insertform').serialize();
e.preventDefault();
swal({
title: "Are you sure",
text: "Do you want to save hotel data",
type: "info",
showCancelButton: true,
closeOnConfirm: false,
showLoaderOnConfirm: true, },
function(){
setTimeout(function(){
$.ajax({
url: "hotels/insert_Hotel.php",
type: "POST",
data: new FormData(this),
async: false,
success: function(data){
swal("Saved! your hotel data has been saved");
location.reload();
},
contentType: false,
cache: false,
processData:false,
error: function(){
alert('error handing here');
}
});
}, 2000);
});
}));
</script>
這是我的PHP文件
<?php
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['logo']['tmp_name'])) {
$randomvalue=rand(1,999999999);
$date=date("Y-m-d H:i:s a");
$sourcePath = $_FILES['logo']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT']."/hotelLogos/".$randomvalue.$date.$_FILES['logo']['name'];
$dbpath="../../hotelLogos/".$randomvalue.$date.$_FILES['logo']['name'];
move_uploaded_file($sourcePath,$targetPath);
}
else{
echo 'file not uploaded';
}
}
?>
它不上傳文件到文件夾,並不會插入值表...
'它返回我空值'你不會返回任何值,除非有錯誤..?我會建議你閱讀這個問題:http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously –
我看到很多錯誤和問題,同時保存圖像與AJAX。我建議你使用fileupload.js(https://blueimp.github.io/jQuery-File-Upload/)通過js上傳文件。最好的。 –