在我描述我的问题之前,我确实在互联网上搜索并在这个网站上找到解决方案。我发现了一个与我的问题非常相似的问题,但是给出的答案甚至不是很接近,所以我来这里写我的问题。
问题是:form
有一些输入字段和input type='file'
用于上传图片。这个表单在jquery-ui dialog
内,当我提交form
时,所有字段都应该响应,除了input type='file'
以外的图片,它永远不会带有任何图像数据。update_form.php:
<form action="" class="fixed-dialog" method="post" id="customForm" enctype="multipart/form-data">
<input type="file" class="form-group input-field" accept="image/jpg,image/png,image/jpeg,image/gif" id="image" name="image" />
</form>
当用户点击Send
按钮时,此代码将触发(JQuery):
$("#send").click(function(){
var all = $("#customForm").serialize();
//the following condition is to check if all input fields were filled.
$.ajax({
type: "POST",
url: "includes/update_user_info.php",
data: all,
success: function(data){
if(data == 1){
alert("You have update data successfuly");
window.location = "index.php";
}else{
alert("there is an error");
}
}
});
});
这里是 update_user_info.php:将图像从tmp文件移动到其他文件
$user_image = &$_FILES['image']['name'];
$user_image_type = &$_FILES['image']['type'];
$user_temp_image = &$_FILES['image']['tmp_name'];
if(isset($_FILES['image'])){
$_SESSION['errors'] = "it's set.\n";
//the next statement is empty, it should carry some data.
$_SESSION['errors'] .= $_FILES['image']['tmp_name'];
}else{
$_SESSION['errors'] = "NOT SET YET!!!";
}
$target = "./images/users_img/".$user_image;
$source = $_FILES['image']['tmp_name'];
// if($_FILES['image']['error'] == 0){
move_uploaded_file($_FILES['image']['tmp_name'],$target);
$_SESSION['errors'] .= "Target is: ".$_FILES['image']['tmp_name'];
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$_SESSION['errors'] .= "Target is: it Worked";
echo 1;
}else{
$_SESSION['errors'] .= "Target is: NOT WORKING";
echo 1;
}
// }else{
// $_SESSION['errors'] .= "Something went Wrong!!";
// echo 1;
// }
当我尝试回复$_FILES['image']['tmp_name']
,或['name']
或['error']
时,它总是给我一个错误:
未定义的索引:name / tmp_name /或错误,
isset($_FILES['image'])
= TRUE。, BUT:
$_FILES['image']['name']
为空。
任何帮助将不胜感激 谢谢。
答案 0 :(得分:1)
我不相信你可以通过这种方式发送文件输入(只需序列化)。
以下是您需要做的事情:
var data = new FormData();
jQuery.each(jQuery('#file')[0].files, function(i, file) {
data.append('file-'+i, file);
});
然后你可以通过ajax发送它们
firstnameInputValue = $('#firstnameInput').val();
jQuery.ajax({
url: 'php/upload.php',
data: {
file: data,
firstname: firstnameInputValue
},
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});
在那里找到它:Sending multipart/formdata with jQuery.ajax
过去对此有所了解。