所以我正在做一个项目,但我一直在尝试上传文件。我已经阅读了多个教程并将代码复制到 netbeans 中,看看它是否会发现任何错误。一切似乎都很好,但是上传后文件不在目标文件夹中,我不知道为什么。
我正在 ajax 发布文件。这是我的 html 和 jQuery 代码:
<html>
<head>
<Script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script language="javascript" type="text/javascript">
function submit(){
var $request;
var $data = new FormData(document.forms.namedItem("upload"));
// abort pending request
if ($request) {
$request.abort();
}
var $form = $("#form");
var $file = $("#file");
// request to /main.php
var $request = $.ajax({
url: "main.php",
type: "post",
data: $data,
processData: false, //tell jQuery not to process the data
contentType: false //tell jQuery not to set the contentType
});
// on success
$request.done(function (response, textStatus, jqXHR){
$("#responseField").html(response).show();
});
//on failure
$request.fail(function (jqXHR, textStatus, errorThrown){
console.error(
"The following error occured: "+
textStatus, errorThrown
);
});
};
</script>
</head>
<body>
<h1>Prototype Upload Page</h1><br/>
<form enctype="multipart/form-data" method="post" id="upload">
Select .csv file: <input type="file" name="file" id="file">
</form>
<input type="submit" id="submit" name="submit" value="submit" onclick="submit()"/>
<br><textarea id="responseField" rows="4" cols="75"> file upload response: </textarea><br/>
</body>
该帖子被发布到我服务器上的 main.php 中。这是代码。
<?php
if (!empty($_FILES["file"]["name"])) {
uploadFile();
}
Function uploadFile() {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else {
echo "Upload: " . $_FILES["file"]["name"] . "\n";
echo "Type: " . $_FILES["file"]["type"] . "\n";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . "kB\n";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "\n";
if (file_exists("uploads/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
}
else {
move_uploaded_file($_FILES["file"]["tmp_name"],"C:/inetpub/wwwroot/test1/afterbreak/uploads/test.csv");
echo "Stored in: " . "uploads/" .$_FILES["file"]["name"];
}
}
}
?>
我无法弄清楚到底是什么问题。我花了很多时间阅读教程,试图让它发挥作用。如果有人可以提供帮助,将不胜感激!