gpt4 book ai didi

javascript - $_FILES 为空且不包含任何数据

转载 作者:太空宇宙 更新时间:2023-11-04 16:21:21 24 4
gpt4 key购买 nike

在描述我的问题之前,我确实在互联网和本网站上进行了搜索以找到解决方案。我发现一个问题与我的问题非常相似,但给出的答案甚至不太接近,所以我在这里写下我的问题。

问题是:
我有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'] ,它总是给我一个错误:
未定义索引:名称/tmp_name/或错误
<强> isset($_FILES['image']) = TRUE。,但是:
<强> $_FILES['image']['name']为空。

任何帮助将不胜感激。
谢谢。

最佳答案

我不相信您可以通过这种方式发送文件输入(仅通过序列化)。

这是你必须做的:

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

过去处理过这个问题。

关于javascript - $_FILES 为空且不包含任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40620354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com