我使用move_uploaded_file遇到了一个问题,特别是它...不起作用。 我有一个输入来上传pdf:
<div>
Upload Resources
<input type="text" id="doc-name" name="doc-name">
<input type="file" id="doc-upload" name="doc-upload" accept="application/pdf">
<button id="upload-button">Upload</button>
</div>
一些jquery使用该文件发送POST请求:
$("#upload-button").click(function() {
if ($('#doc-name').val() == '') {
var formData = new FormData();
var filename = $('#doc-name').val();
var uploaded = $('#doc-upload').val().split('\\');
var path = uploaded[uploaded.length - 1];
var file = $('#doc-upload').prop('files')[0];
formData.append("functionCall", "document-upload");
formData.append("filename", filename);
formData.append("path", path);
formData.append("file", file);
$.ajax({
url: '/assets/functions.php',
type: 'POST',
data: formData,
contentType: false,
processData: false,
success: function(e) {
console.log(e);
},
error: function(jqXHR, textStatus, errorMessage) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorMessage);
}
});
)};
这发送到我的函数文件,这个文件设置有点奇怪(它可能实际上是标准的做法,我从来没有见过这样的)因为它是一个很大的switch语句;哪个case运行依赖于传入的functionCall变量。所以这里是该特定case语句的函数代码:
.
.
.
case "doc-upload":
$filename = $_POST['filename'];
$path = $_POST['path'];
$file = $_FILES['file'];
$uploadQuery = "INSERT INTO uploads (filename, path) VALUES ('$filename', '$path')";
$upload = mysql_query($uploadQuery);
if (move_uploaded_file($_FILES['file']['tmp_name'], 'test/' . $_FILES['files']['name'])) {
echo 'yay';
} else {
echo 'such is life';
}
move_uploaded_file行之上的所有内容都很有效,但我一直在返回“这样是生命”行,并且传输的文件没有显示在'test /'中。由于file_exists检查,我知道测试文件夹和上传文件都存在。关于我可能做错了什么的任何想法?