$_FILES empty and does not contain any data
本问题已经有最佳答案,请猛点这里访问。
在我描述我的问题之前,我确实在互联网上和这个网站上搜索来找到解决方案。我发现了一个和我的问题非常相似的问题,但给出的答案甚至不接近,所以我来这里写我的问题。
问题是:我有
更新u form.php:
1 2 3 | <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> |
当用户点击
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $("#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文件移动到其他文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | $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. "; //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; // } |
当我试图回应
任何帮助都将不胜感激。谢谢您。
我不相信您可以用这种方式发送文件输入(只是通过序列化)。
以下是您必须做的:
1 2 3 4 | var data = new FormData(); jQuery.each(jQuery('#file')[0].files, function(i, file) { data.append('file-'+i, file); }); |
然后你可以通过Ajax发送它们
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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); } }); |
找到它:用jquery.ajax发送multipart/formdata
以前处理过这个问题。