使用Ajax上传图片但PHP没有看到文件 [英] Image upload with Ajax but PHP does not see file

查看:134
本文介绍了使用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回复){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_forclass =form-horizo​​ntal col-lg-8 dblue_textrole =formaction =../ php / profile_image.phpmethod = postenctype =multipart / form-data> < div class =form-group> < label class =control-label col-sm-2for =info_abo>< i class =fa fa-info>< / i>关于< /标签> < div class =col-lg-8 col-sm-10> < textarea class =form-controlid =info_aboname =info_aboplaceholder =让您的朋友了解一下自己!><?php echo $ info_abo;?>< / textarea> ; < / DIV> < / DIV> < div class =form-group> < i class =fa fa-picture-o>< / i>< label class =control-label col-sm-2for =file>< / i>化身LT; /标签> < div class =col-lg-8 col-sm-10> < input type =filename =fileid =file> < / DIV> < / div>< / form>< button onclick =info_save()class =btn btn-info>更新< / 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返回代码:。 $ _FILES [file] [error]。 <峰; br /><峰; br /> 中; } else {if(file_exists(../ profiles /\".$ log_username。/。$ _ FILES [file] [name])){echoFile Already Exists:。$ _ FILES [file ][名称]; } else {$ sourcePath = $ _FILES ['file'] ['tmp_name']; //将文件的源路径存储在变量中$ targetPath =../profiles/\".$log_username.\"/\"._FILES['file']['name']; //将文件存储到的目标路径if(move_uploaded_file($ sourcePath,$ targetPath)){echo'success |'。$ _ FILES [file] [name]; exit();} else {echo'Connection Error'; exit();}}}} else {echoInvalid file Size or Type;出口(); }} else {echo'success | na'; exit();}} else {echo'请登录'; ();}?>>  

div>

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

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



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

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


So the PHP works fine when I just the form in a standard way. But, when I try to send the form through ajax, it does not see the $_file[] and returns, "success|na" which I want it to do if there is no file, but I get the same result if there is a file.

Are the ajax setting correct? cashe: false, processData: false?

Any help would be helpful guys I can always use an iframe, but I would rather not. Thank you for time!

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();}
?>

解决方案

The FormData Constructor takes an HTMLFormElement as the parameter not a selector string or a jQuery object.

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

or

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

or

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

这篇关于使用Ajax上传图片但PHP没有看到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆