如何在php中读取FormData对象 [英] how to read FormData object in php

查看:285
本文介绍了如何在php中读取FormData对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

    <?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>

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

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

推荐答案

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

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.

您应该在<$中找到您的资料c $ c> $ _ POST ['file'] $ _ FILES ['file'] 。下次在提交后尝试 var_dump($ _ POST); 查看数组。 :)

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

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

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

这篇关于如何在php中读取FormData对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆