gpt4 book ai didi

PHP文件上传未定义索引错误

转载 作者:行者123 更新时间:2023-12-01 04:49:27 25 4
gpt4 key购买 nike

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

不幸的是,每次我尝试验证文件时,我都会收到“未定义索引”错误,这意味着 $_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');
}

}
});

感谢您的浏览!

最佳答案

问题在于你如何上传它。 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
});

关于PHP文件上传未定义索引错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22998867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com