【问题标题】:PHP File Upload Undefined Index errorPHP文件上传未定义索引错误
【发布时间】:2014-05-24 19:10:30
【问题描述】:

我对此感到非常困惑。我有一个表格,用户可以选择上传简历……很简单。

不幸的是,每次我尝试验证文件时,都会不断收到“未定义索引”错误,这意味着 $_FILES[] 数组为空。我已经提高了我的upload_max_filesizepost_max_size 并确保在我的 php.ini 文件中打开了文件上传并重新启动了 apache,但数组仍然返回空。

这是我的表单 HTML:

<form enctype="multipart/form-data" class="form-horizontal" id="contact-form" name="contact-form" action="/includes/mail/" method="post">
    <div id="resume-input" class="form-group">
        <label class="control-label col-sm-2">Upload Resume</label>
        <div class="col-sm-10">
            <input type="file" name="resume" id="resume-file" class="form-control" />
        </div>
    </div>
</form>

这里是 PHP 检查文件:

if(!isset($_FILES['resume'])) {
    echo "<span class='error'>Please upload your resume.</span><br />";
    return false;
} else {
    // Validate uploaded file
    $fileName = $_FILES['resume']['name']; // file name
    $fileExt = substr($fileName, strrpos($fileName, '.') + 1); // file extension
    $fileSize = $_FILES['resume']['size']/1024; // size in KBs
    $filePath = $_FILES['resume']['tmp_path']; // file path
}

显然这不是整个脚本,但这是唯一不起作用的部分。我在脚本开头尝试了var_dump($_FILES);,它返回array(0) { }

谁能从我发布的内容中看出为什么此文件上传不起作用?

PS: 表单正在通过 jQuery AJAX 提交。我不知道是否有必要,但这里是 AJAX 提交:

$.ajax({
    type: "POST",
    url: url,
    data: contactForm.serialize(), // serializes the form's elements.
    success: function(data) {
        returnMsg.fadeIn();
        returnMsg.html(data); // show response from the php script.
        if(data.indexOf("success") + 1) {
            $('form#contact-form input[type="text"],input[type="email"],textarea').val('');
            $('form#contact-form select[name="subject"]').val('Select a subject');
        } 

    }
});

感谢观看!

【问题讨论】:

  • [点击这里][1]看答案………………[1]:stackoverflow.com/questions/2320069/jquery-ajax-file-upload
  • 检查var_dump($_SERVER['REQUEST_METHOD']),确保上面写着POST。如果它显示GET,那么您已经发生了一个完整的 HTTP 重定向,并作为 GET 请求登陆您的上传脚本,这意味着上传丢失。因为就像现在写的那样,您的 html 看起来不错,并且您的 php 应该可以正常工作。另外,您不能通过标准 AJAX 上传文件。

标签: php jquery ajax forms file-upload


【解决方案1】:

问题在于您如何上传它。 data: contactForm.serialize() 只是不适用于文件。你得到了正确的表单,但是通过用 jQuery AJAX 请求替换它,你完全改变了请求。

可以使用AJAX上传HTML5文件,不需要表单:

document.querySelector('#resume-file').addEventListener('change', function(e) {
    var file = this.files[0];
    var fd = new FormData();
    fd.append("resume", file);
    var xhr = new XMLHttpRequest();
    xhr.open('POST', url, true);

    xhr.onload = function() {
        if (this.status == 200) {
            // success!!!
        }
    }

    xhr.send(fd);
}

欲了解更多信息,请参阅:MDN - Using FormData objects

编辑:

这也是在 jQuery 中的操作方法(取自 MDN 文档):

var fd = new FormData(document.getElementById("fileinfo"));
fd.append("CustomField", "This is some extra data");
$.ajax({
  url: "stash.php",
  type: "POST",
  data: fd,
  processData: false,  // tell jQuery not to process the data
  contentType: false   // tell jQuery not to set contentType
});

【讨论】:

  • 太棒了!谢谢你。我不知道我们不能通过 jQuery AJAX 上传文件
  • 没问题。实际上可以在 jQuery 中完成,只是选项略有不同。它解释了我发布的链接中的方式,但我也会在答案中引用它。干杯。
  • 使用 XMLHTTPRequest 对象的最大优势之一是能够监听上传进度事件,这允许您拥有进度条。
猜你喜欢
  • 2014-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-13
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多