我有像這樣一個簡單的PHP文件上傳腳本,圖像不保存,當我加入了Ajax代碼
<?php
$path = 'uploads/';
$file_ext = array('jpg','png','gif','bmp','JPG');
$post_ext = end(explode('.',$_FILES['photo']['name']));
$photo_name = $_FILES['photo']['name'];
$photo_type = $_FILES['photo']['type'];
$photo_size = $_FILES['photo']['size'];
$photo_tmp = $_FILES['photo']['tmp_name'];
$photo_error= $_FILES['photo']['error'];
//move_uploaded_file($photo_tmp,"uploads/".$photo_name);
echo $photo_tmp;
if((($photo_type == 'image/jpeg') || ($photo_type == 'image/gif') ||
($photo_type == 'image/png') || ($photo_type == 'image/pjpeg')) &&
($photo_size < 2000000) && in_array($post_ext,$file_ext)) {
/* Understand in-Array !! */
if($photo_error > 0){
echo 'Error '.$photo_error;
exit;
}else{
echo $photo_name.' Uploaded !';
}
if(file_exists($path.$photo_name)){
echo 'There is '.$photo_name;
}else{
//new photo name and encryption
$new_name = explode('.',$photo_name);
$photo_name = 'erkan_'.md5($new_name[0]).'.'.$new_name[1];
//move to directory
if(move_uploaded_file($photo_tmp,$path.$photo_name)){
return $photo_name;
}
}
}
?>
表單代碼我的文件被保存完美的罰款,但是當我加入少許AJAX的組合,像這樣:
$(function(){
$('button[type="submit"]').on('click' , function(e){
e.preventDefault();
var formData = new FormData();
formData.append('photo', $('input[type=file]')[0].files[0]);
$.ajax({
url: 'fileupload.php',
data: formData,
// THIS MUST BE DONE FOR FILE UPLOADING
contentType: false,
processData: false,
// ... Other options like success and etc
});
});
});
現在,當我上傳圖片時,圖片未保存在我的上傳文件夾中,爲什麼?
請同時顯示'
'。 –你會得到什麼錯誤?任何控制檯日誌? – rt2800
@NanaPartykar完成! –