文件上传$ _FILES获取空值 [英] File Upload $_FILES getting empty value
本文介绍了文件上传$ _FILES获取空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在html文件输入类型更改并且应该上载文件时触发ajax表单提交。但在PHP文件中,文件名为空。
HTML
< 表格 id = UploadForm 方法 = post enctype = multipart / form-data >
< 输入 type = file 跨度> <温泉n class =code-attribute> id = upload 名称 = 上传 >
< / form >
Javascript:
unction uploadimg(){
$ .ajax( {
url: uploadImg.php,
data:{},
成功: function (响应){
console .log(响应);
if (response == success ){
alert( Image Uploadedloaded Successfully);
}
else if (response == 无效){
alert( 无效的文件类型);
}
else {
alert( 图片未上传);
}
}
});
}
PHP:
$ allowedExts = array( gif, jpeg, jpg, png) ;
// $ temp = explode(。,$ _FILES [upload] [name ]); // image
// $ extension = end($ temp);
echo $ _FILES [ upload] [ name];
$ ext = pathinfo($ _ FILES [ upload] [ name],PATHINFO_EXTENSION);
echo ------ --------------。$ ext。 - -----------------------跨度>;
$ src = ;
if (($ $ _FILES [ 上传] [ type] == image / gif)
||($ _FILES [ upload] [ type ] == image / jpeg)
||($ _FILES [ upload] [ type] == image / jpg)
||($ _FILES [ upload] [ type] == image / pjpeg)
|| ($ _FILES [ upload] [ type] == image / x- png)
|| ($ _FILES [ upload] [ type] == image / png ))
&& in_array($ extension,$ allowedExts)){
$ filename = $ _SESSION [' < span class =code-string> username']。 $ _FILES [' upload'] [' name'];
move_uploaded_file($ _ FILES [' upload'] [' tmp_name'], upload /。$ filename);
// 更新查询
$ query = update js_basic_details set image ='。$ filename。 'where email_id ='。$ _ SESSION [' username']。 ';
if(mysql_query($ query))
return 成功跨度>;
else
return 错误;
}
else {
echo 无效 跨度>;
}
解决方案
.ajax({
url: uploadImg.php,
data:{},
成功: function (响应){
console .log(响应);
if (response == success){
alert( 图片上传成功);
}
else if (response == 无效){
alert( 无效的文件类型);
}
else {
alert( 伊玛ge未上传);
}
}
});
}
PHP:
allowedExts = array( gif, jpeg, jpg, PNG跨度>);
//
temp = explode(。,
I want to fire an ajax form submit when an html file input type changes and the file should be uploaded. but in the PHP file the filename is empty.
HTML
<form id="UploadForm" method="post" enctype="multipart/form-data">
<input type="file" id="upload" name="upload" >
</form>
Javascript:
unction uploadimg(){
$.ajax({
url:"uploadImg.php",
data:{},
success:function(response){
console.log(response);
if(response=="success"){
alert("Image Uploaded Successfully");
}
else if(response=="Invalid"){
alert("Invalid File Type");
}
else{
alert("Image Not Uploaded");
}
}
});
}
PHP:
$allowedExts = array("gif", "jpeg", "jpg", "png");
//$temp = explode(".", $_FILES["upload"]["name"]); //image
//$extension = end($temp);
echo $_FILES["upload"]["name"];
$ext=pathinfo($_FILES["upload"]["name"],PATHINFO_EXTENSION);
echo "--------------------".$ext."-------------------------";
$src="";
if ((($_FILES["upload"]["type"] == "image/gif")
|| ($_FILES["upload"]["type"] == "image/jpeg")
|| ($_FILES["upload"]["type"] == "image/jpg")
|| ($_FILES["upload"]["type"] == "image/pjpeg")
|| ($_FILES["upload"]["type"] == "image/x-png")
|| ($_FILES["upload"]["type"] == "image/png"))
&& in_array($extension, $allowedExts)) {
$filename = $_SESSION['username']. $_FILES['upload']['name'];
move_uploaded_file($_FILES['upload']['tmp_name'],"upload/". $filename);
//Update Query
$query="update js_basic_details set image='".$filename."' where email_id='".$_SESSION['username']."'";
if(mysql_query($query))
return "success";
else
return "Error";
}
else{
echo "Invalid";
}
解决方案
.ajax({ url:"uploadImg.php", data:{}, success:function(response){ console.log(response); if(response=="success"){ alert("Image Uploaded Successfully"); } else if(response=="Invalid"){ alert("Invalid File Type"); } else{ alert("Image Not Uploaded"); } } }); }
PHP:
allowedExts = array("gif", "jpeg", "jpg", "png"); //
temp = explode(".",
这篇关于文件上传$ _FILES获取空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文