因此,我查找了a tutorial上传文件并使用XML HTTP请求将文件发送到服务器。我跟着教程,但是,我认为我必须错过一些东西。当文件似乎被上传和发送时,“处理器”文件中的任何内容都不会被访问。有没有我需要编写的PHP函数来处理它?对于上下文,这里是我写的:需要帮助处理XML HTTP文件上传请求
$(document).ready(function()
{
$('#upload-button').click(function(event)
{
$('#upload-button').removeClass("btn-danger");
});
$("#report-form").submit(function(event)
{
var form = document.getElementById('report-form');
var fileSelect = document.getElementById('file-select');
var uploadButton = document.getElementById('upload-button');
event.preventDefault(); // Stop the event from sending the way it usually does.
uploadButton.value = 'Submitting...'; // Change text.
var files = fileSelect.files;
var maxfiles = <?php echo $config['Report_MaxFiles'] ?>;
var mfs = <?php echo $config['Report_MaxFileSize'] ?>;
if(files.length > maxfiles) // Make sure it's not uploading too many.
{
uploadButton.value = 'You uploaded too many files. The limit is ' + maxfiles + '.'; // Update button text.
$('#upload-button').addClass('btn-danger'); // Make the button red, if so.
return;
}
var formData = new FormData(); // Make a "form data" variable.
for (var i = 0; i < files.length; i++) {
var file = files[i];
// Add the file to the request.
if(file.size/1000 > mfs)
{
uploadButton.value = 'One of the files is too big. The file size limit is ' + (mfs) + 'kb (' + (mfs/1000) + 'mb).';
$('#upload-button').addClass('btn-danger');
return;
}
formData.append('files[]', file, file.name); // Not really sure what this does, to be honest,
// but I think it makes a file array.
}
var xhr = new XMLHttpRequest(); // Construct an XML HTTP Request
xhr.open('POST', 'assets/class/FileHandler.php', true); // Open a connection with my handler PHP file.
xhr.onload = function()
{
if (xhr.status === 200)
{
uploadButton.value = 'Files Submitted!'; // NOTE: I do get this message.
}
else
{
uploadButton.value = 'An error occurred.';
$('#upload-button').addClass("btn-danger");
}
};
xhr.send(formData); // I think this is where it dies.
});
});
在“发送(FORMDATA)”行,我实际上并不知道它的发送。我是否在FileHandler.php中设置了某种侦听器,并在通过XML HTTP请求发送文件时激活它们?或者更具体地说,如何使用我的FileHandler.php文件将上传的文件保存到服务器?
编辑:我一直没能拿出在FileHandler.php文件比这个,我认为可能被称为当发送的任何其它的PHP代码(但它不是):
编辑2:好吧,现在我有东西,但它不工作(没想到它)。我想我可能是错的使用变量:
<?php
$uploaddir = 'data/reports/uploads/' . $_POST['id'] . "/";
$uploadfile = $uploaddir . basename($_FILES['files']['name']);
echo "<script>console.log('RECEIVED');</script>";
echo '<pre>';
if (move_uploaded_file($_FILES['files']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
它不保存文件的目录,也不是打印文字讯息。我如何让我的report.php文件在FileHandler.php中执行这些事情?
什么是您的PHP代码? – 2015-03-31 20:41:43
就是这样:我没有。 – ColonelHedgehog 2015-03-31 20:43:55
您需要一些PHP来在服务器端有效地保存文件,如教程所说:“您的服务器端代码需要从请求中提取文件并根据需要处理它们。” – 2015-03-31 20:46:43