douhoulei4706 2018-06-14 13:36
浏览 125
已采纳

使用Ajax发送表单数据时PHP不会获得$ _FILES

I'm trying to create an upload element on my website. What I'm trying to do is an upload button that instantly when the user picks a file from the computer it uploads it to the server.

This is my form:

<form id="createAlert" name="createAlert" enctype="multipart/form-data" method="POST" action="createAlert.php">
    <input name="title" type="text" class="input-medium" maxlength="36"></input>

    <textarea name="content" rows="7" cols="90" class="input-short" style="font-family: arial; width: 80%; resize: none; height: 250px"></textarea>

    <input name="push" type="checkbox"></input>

    <input enctype="multipart/form-data" name="img" id="img" size="35" type="file"/>

    <input class="submit-gray" type="submit" value="POST ALERT"/>
</form>

Here is my Javascript code that sends the file as a FormData object to an upload page (upload.php):

    $('#img').live('change', function() {
        var formData = new FormData();
        formData.append('file', $('#img')[0].files[0]);
        $.ajax({
            url : 'upload.php',
            type : 'POST',
            data : formData,
            enctype: 'multipart/form-data',
            success : function(data) {
                console.log(data);
                alert(data);
            },
            cache: false,
            contentType: false,
            processData: false
        });
    });

So far, so good - everything works fine. The problem is with the upload.php file that receives the FormData. Here is it's code (It's only a testing version it doesn't upload the file yet):

<?php
print file_get_contents('php://input');
var_dump($_FILES);
var_dump($_POST);

The problem is that the output of var_dump($_FILES); and var_dump($_POST) is two empty arrays, while in the file_get_contents('php://input') I get the file data.

Here is the output (I cut out the part with the content of the uploaded file...):

------WebKitFormBoundaryw4nmFcISqYuAWQOS
Content-Disposition: form-data; name="file"; filename="Sem Título-1.png"
Content-Type: image/png

//Here is the file I uploaded...

------WebKitFormBoundaryw4nmFcISqYuAWQOS--
array(0) {
}
array(0) {
}

I read dozens of answers to questions here and many solutions to the problem I have but none of them solved the problem.

What am I doing wrong? Why am I receiving the file just in the php://input but not using $_FILES?

Thank you!

  • 写回答

1条回答 默认 最新

  • duanfei1975 2018-06-14 14:37
    关注

    I would suggest you update the version of jQuery that you're using. live() was deprecated and removed from the source over 5 years ago now, and there have been several changes to the way data is serialised and encoded within $.ajax().

    It's possible the old version you're using doesn't have these updates and is not adding the FormData to the request in the correct manner.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。