php 如何在php中读取FormData对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17834243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:32:18  来源:igfitidea点击:

how to read FormData object in php

phpjavascriptjquery

提问by Anurag Singh

Hello everyone I am posting a sample code in which I am uploading a file using Ajax JQuery. All thing works fine but I want to read the file content in my php code. So what is the syntax to read it?

大家好,我发布了一个示例代码,其中我使用 Ajax JQuery 上传文件。一切正常,但我想在我的 php 代码中读取文件内容。那么读取它的语法是什么?

    <?php

?>

<!--================================html==================================================-->

<html>
<head>
    <title>AJAX UPLOAD</title>
    <script type="text/javascript" src="jquery-2.0.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $('#Button_').click(function(){
                alert("hi");
                var name= document.getElementById('File_');
                 var alpha=name.files[0];
                console.log(alpha.name);
                 var data= new FormData();
                 data.append('file',alpha);
                 $.ajax({
                 url:'process.php',
                 data:data,
                 processData:false,
                 contentType:false,
                 type:'POST',
                 success:function(msg){
                 alert(msg);
                 }
                 });
            });
        });
    </script>
</head>
<body>
    <input type="file" name="File" id="File_"/>
    <input type="button" name="Button" id="Button_" value="UPLOAD">
</body>
</html>

Now I do not know how to read the file data sent via Ajax. So please let me know the code

现在我不知道如何读取通过 Ajax 发送的文件数据。所以请让我知道代码

回答by

The first argument you pass to FormData.append()is the name of the form element that is passed to the server. So data.append('file', alpha)in your case is equivalent to <input type="file" name="file">- you passed 'file' to append() so 'file' is your input name.

您传递给的第一个参数FormData.append()是传递给服务器的表单元素的名称。所以data.append('file', alpha)在你的情况下相当于<input type="file" name="file">- 你将 'file' 传递给 append() 所以 'file' 是你的输入名称。

You should find your stuff in $_POST['file']and $_FILES['file']. Next time try var_dump($_POST);after submit to see the array. :)

你应该在$_POST['file']和 中找到你的东西$_FILES['file']。下次var_dump($_POST);提交后尝试查看数组。:)

Learn about file uploads in php here: http://php.net/manual/en/features.file-upload.php

在此处了解 php 中的文件上传:http: //php.net/manual/en/features.file-upload.php