我正在尝试使用 Ajax 和 PHP 上传文件,但问题是我无法发送包含文件夹 ID 的输入类型文本。
这是我的 HTML:
<form method="post" enctype="multipart/form-data" action="uploadify.php">
<input type="file" name="images" id="images" multiple />
<input type="hidden" name="eid" value="<?php echo $eid;?>" />
<input type="btn submit" id="btn" value="Upload" />
</form>
<div id="response"></div>
<ul id="image-list">
</ul>
这是我的 JavaScript:
(function () {
var input = document.getElementById("images"),
formdata = false;
function showUploadedItem (source) {
var list = document.getElementById("image-list"),
li = document.createElement("li"),
img = document.createElement("img");
img.src = source;
li.appendChild(img);
list.appendChild(li);
}
if (window.FormData) {
formdata = new FormData();
document.getElementById("btn").style.display = "none";
}
input.addEventListener("change", function (evt) {
document.getElementById("response").innerHTML = "Uploading . . ."
var i = 0, len = this.files.length, img, reader, file;
for ( ; i < len; i++ ) {
file = this.files[i];
if (!!file.type.match(/image.*/)) {
if ( window.FileReader ) {
reader = new FileReader();
reader.onloadend = function (e) {
showUploadedItem(e.target.result, file.fileName);
};
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("images[]", file);
}
}
}
if (formdata) {
$.ajax({
url: "uploadify.php",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
document.getElementById("response").innerHTML = res;
}
});
}
}, false);
}());
这是我的PHP:
<?php
//upload.php
$eid = $_POST['eid'];
$output_dir = "./images/icons/".$eid."/screenshots/";
if (!file_exists($output_dir) and !is_dir($output_dir)) {
mkdir($output_dir);
}
foreach ($_FILES["images"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["images"]["name"][$key];
move_uploaded_file( $_FILES["images"]["tmp_name"][$key], "$output_dir" . $_FILES['images']['name'][$key]);
}
}
echo "<h2>Successfully Uploaded Images $eid</h2>";
我想要做的是我正在发送输入类型 hidden name=eid 但不知道出了什么问题,为什么我不能通过我的 JS 函数发送这个?