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

时间:2016-04-13 21:11:42

标签: php jquery ajax forms

因此,当我以标准方式使用表单时,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');
					}
				}
			});
}
<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();}
?>

1 个答案:

答案 0 :(得分:1)

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

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

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

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