最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to read FormData object in php - Stack Overflow

programmeradmin0浏览0评论

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>

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

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>

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

Share Improve this question edited Nov 9, 2017 at 9:19 Cash Dogg 1751 gold badge2 silver badges11 bronze badges asked Jul 24, 2013 at 12:36 Anurag SinghAnurag Singh 7276 gold badges10 silver badges27 bronze badges 2
  • You read it just like you would if a form were being submitted. With $_POST[] and $_FILES[]. – Barmar Commented Jul 24, 2013 at 12:38
  • i added $name=$_POST[]; now what parameter i have to pass in this bracket. – Anurag Singh Commented Jul 24, 2013 at 12:41
Add a comment  | 

1 Answer 1

Reset to default 28

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.

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

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

发布评论

评论列表(0)

  1. 暂无评论