使用Ajax上传图像,但PHP看不到文件


Image upload with Ajax but PHP does not see file

因此,当我以标准方式处理表单时,PHP运行良好。但是,当我尝试通过ajax发送表单时,它看不到$_file[],并返回"success|na"如果没有文件,我希望它这样做,但如果有文件,我会得到相同的结果。

ajax设置正确吗?cash: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');
					}
				}
			});
}
<form id="info_for" class="form-horizontal col-lg-8 dblue_text" role="form" action="../php/profile_image.php" method="post" enctype="multipart/form-data">
	<div class="form-group">
		<label class="control-label col-sm-2" for="info_abo"><i class="fa fa-info"></i> About</label>
		<div class="col-lg-8 col-sm-10">
			<textarea class="form-control" id="info_abo" name="info_abo" placeholder="Let your friends know a little about yourself!"><?php echo $info_abo;?></textarea>
		</div>
	</div>
	<div class="form-group">
		<label class="control-label col-sm-2" for="file"><i class="fa fa-picture-o"></i> Avatar</label>
		<div class="col-lg-8 col-sm-10"> 
			<input type="file" name="file" id="file">
		</div>
	</div>
</form>
<button onclick="info_save()" class="btn btn-info">Update</button>
<?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"] < 100000) && in_array($file_extension, $validextensions)){
				if ($_FILES["file"]["error"] > 0){
					echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>";
				}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')),