php怎么接收ajax上传的文件

<input type="file" id="filename" name="filename" >
        <button>上传</button>

        <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
        <script>
            $("button").click(function() {
                $("button").click(function(event) {
                    var formData = new FormData();    
                    formData.set("file",document.getElementById("filename").files);
                    console.log(formData.get("file"))
                    $.ajax({
                        url: 'demo.php',
                        type: "POST",
                        data:formData,
                        cache:false,         //不设置缓存
                        processData: false,  // 不处理数据
                        contentType: false   // 不设置内容类型
                    })
                    .done(function() {
                        console.log("success");
                    })
                    .fail(function() {
                        console.log("error");
                    })
                    .always(function() {
                        console.log("complete");
                    });
                });
                
            })

PHP代码

    move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);

上传后没有看到文件新增,求解决,谢谢!

阅读 4.2k
3 个回答

可以用 file_get_contents("php://input"); 接收

form表单是否有增加enctype=”multipart/form-data”

这个肯定是没有发过去了。。你自己看下请求不就知道了吗

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题