使用Ajax上传图像但PHP没有看到文件

因此,当我以标准方式使用表单时,PHP工作正常。 但是,当我尝试通过ajax发送表单时,它没有看到$ _file []并返回,“success | na”如果没有文件我想要它做,但是如果有的话我会得到相同的结果一份文件。

ajax设置是否正确? cashe:false,processData:false?

任何帮助都会有帮助,我总是可以使用iframe,但我宁愿不这样做。 谢谢你的时间!

function info_save(){ gf.wait(); var info_ava = ''; var info_abo = $('#info_abo').val() target = './php/profile_system.php'; $.ajax({ url: "./php/profile_image.php", type: "POST", data: new FormData('#info_for'), contentType: false, cache: false, processData:false, success: function(reply){ imgresponse = reply.split("|"); if(imgresponse[0] == 'success' || imgresponse[0] == 'Success'){ if(imgresponse[1] == 'na'){info_ava = ' ';} else{info_ava = imgresponse[1];} var formDataA = { 'action': 'info', 'info_abo': info_abo, 'info_ava': info_ava }; profile_ajax(target, formDataA,'info'); }else{ profile_stop(reply,'info'); } } }); } 
 
<?php if(ISSET($log_username) && $user_ok == true){ if(isset($_FILES["file"]["type"])){ $validextensions = array("jpeg", "jpg", "png","gif"); $temporary = explode(".", $_FILES["file"]["name"]); $file_extension = end($temporary); if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/gif")) && ($_FILES["file"]["size"] 0){ echo "Return Code: " . $_FILES["file"]["error"] . "

"; }else{ if (file_exists("../profiles/".$log_username."/".$_FILES["file"]["name"])){ echo "File Already Exists: ".$_FILES["file"]["name"]; }else{ $sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable $targetPath = "../profiles/".$log_username."/".$_FILES['file']['name']; // Target path where file is to be stored if(move_uploaded_file($sourcePath,$targetPath)){echo 'success|'.$_FILES["file"]["name"]; exit();} else{echo 'Connection Error';exit();} } } }else{ echo "Invalid file Size or Type"; exit(); } }else{echo 'success|na'; exit();} }else{echo 'Please Sign In'; exit();} ?>

FormData构造函数将HTMLFormElement作为参数而不是选择器字符串或jQuery对象。

 data: new FormData(document.getElementById('info_for')), 

要么

 data: new FormData($('#info_for')[0]), 

要么

 data: new FormData(document.querySelector('#info_for')),