douchuang4181 2017-11-07 11:53
浏览 43
已采纳

当POST返回NULL时,Var_dump表单数据

I saw some simple code for using formdata for uploading images over ajax but on php side it is showing up NULL inside $_POST, I tried to echo $_POST['file']['name'] but it echos nothing...

My php file has:

var_dump($_POST);

How can I use formData sent by ajax to php (in php)?

$(document).on('change','#image',function(){
    var fd = new FormData();    
        fd.append( 'file', $(this).prop("files")[0]);
       console.log(fd);
    $.ajax({
        type: 'post',
        processData: false,
        contentType: false,
        url: './images.php',
        data: fd,
        success: function(a){
            console.log(a);
        }
    });
 });
#image{display:none}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<label for="image"><i class="fa fa-camera" aria-hidden="true"></i></label><input  type="file" name="file" id="image">

</div>
  • 写回答

2条回答 默认 最新

  • drymoeuka282427675 2017-11-07 11:58
    关注

    You have to use the superglobal $_FILES instead of $_POST try:

    var_dump($_FILES);
    

    Output something like:

     array (size=1)  'file' =>
          array (size=5)
                'name' => string '49e9c80947764261bbd9d46a8063c3d1.jpg' (length=36)      
                'type' => string 'image/jpeg' (length=10)
                'tmp_name' => string 'C:\xampp\tmp\php64A9.tmp' (length=24)      
                'error' => int 0
                'size' => int 3090
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程