我目前正在编写一个网站,该网站确实将文件上传到服务器。我正在尝试通过 Ajax 提交它们。不知何故,这不起作用,我不知道如何解决这个问题。
这是表格的样子
的HTML
演出日期
</p>
<p>
Which Show<br/>
<input type="text" class="span8" id="show" /></p>
<p>
Name of the Presenter<br/>
<input type="text" class="span8" id="presenter" /></p>
<p>
Program Description <br/>
<input type="text" class="span8" id="description" /></p>
<p>
Upload your file <br />
<input type="file" id="file" name="files" />
</p>
<p>
<input type="submit" name="submit" value="Upload your data" />
</p>
JavaScript/jQuery
$('input[type="submit"]').click(function(event){
var _date = $('#date').val();
var _show = $('#show').val();
var _presenter = $('#presenter').val();
var _desc = $('#description').val();
var _file = $('#file').val();
event.preventDefault();
$.post('ajax/submit.php',{"date": _date,
"show": _show,
"presenter": _presenter,
"description": _desc,
"files": _file},
function(data){
console.log(data);
})
return false;
})
PHP的
<?php
if(isset($_POST)){
// Post variables
$date = $_POST['date'];
$show = $_POST['show'];
$presenter = $_POST['presenter'];
$desc = $_POST['description'];
$file = $_FILES["files"];
// config for file handling (where to put)
$upload_dir = "/opt/files/mdb/";
$upload = $upload_dir.basename($_FILES['files']);
// only allowing the filetpyes within the array
$allowed_filtypes = array('audio/mpeg');
// Check if there is any error with uploading the file
if($_FILES["files"]["error"] > 0){
throw new exception('Error encountered');
}else{
if(in_array($_FILES['files']['type'],$allowed_filtypes)){
move_uploaded_file($file, $upload);
}else{
print_r($_FILES);
print_r($_POST);
}
}
}else{
echo "Wrong form key";
// throw new exception('Wrong form key');
}
?>
当我通过 print_r() 打印出数组时,POST 数组中充满了我的数据,但我的 FILES 数组为空。
我认为 jQuery 存在某种问题,但到目前为止我找不到任何有用的东西。
任何帮助表示赞赏