[英]Issue on Uploading Multiple Images to Server By One Click
我想通過一次點擊將多個圖像上傳到服務器。 我沒有在JavaScript控制台上收到任何錯誤,但沒有圖像上傳到服務器。 你能告訴我我做錯了什么嗎?
這是我的HTML標記
<form id="uploadimage" action="" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-xs-6 col-md-3 text-center">
<a href="#" class="thumbnail ">
<div class="img-box">
<input type="file" class="file" name="image" /> <img class="img img-responsive" />
</div>
</a>
</div>
<div class="col-xs-6 col-md-3 text-center">
<a href="#" class="thumbnail ">
<div class="img-box">
<input type="file" class="file" name="image" />
<img class="img img-responsive" />
</div>
</a>
</div>
</div>
<input type="submit" value="Upload" class="submit" />
</form>
js文件為:
$("#uploadimage").on('submit', (function(e) {
e.preventDefault();
$.ajax({
url: "loader.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function(data) {
}
});
}));
和PHP(loader.php)一樣
<?php
if (isset($_FILES["file"]["type"])) {
$validextensions = array(
"jpeg",
"jpg",
"png"
);
$temporary = explode(".", $_FILES["file"]["name"]);
$file_extension = end($temporary);
if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg")) && ($_FILES["file"]["size"] < 100000) //Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>";
} else {
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " <span id='invalid'><b>already exists.</b></span> ";
} else {
$sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
$targetPath = "upload/" . $_FILES['file']['name']; // Target path where file is to be stored
move_uploaded_file($sourcePath, $targetPath); // Moving Uploaded file
echo "<span id='success'>Image Uploaded Successfully...!!</span><br/>";
}
}
} else {
echo "<span id='invalid'>***Invalid file Size or Type***<span>";
}
}
?>\
請參考這個 。
當你發送多個文件時,php接受它作為一個文件數組,但在你的代碼中,你接受它們作為一個單獨的文件。 另外,嘗試將名稱數組作為
<input type="file" name="images[]" id="images" multiple >
$_FILES["image"]
來訪問你的文件。 []
名稱(至少在php中)。 因此,在html源代碼中重命名您的image[]
。 首先,您在控制器中接收的文件數組是錯誤的,
$temporary = explode(".", $_FILES["file"]["name"]);
你需要拍攝圖像而不是文件,因為在你使用的表格文件輸入名稱是圖像所以,
$temporary = explode(".", $_FILES["image"]["name"]);
然后在你的html之后,你可以像thisisme_22那樣建議
<input type="file" name="images[]" id="images" multiple >
然后在你的動作php文件后你可以統計文件和使用for或foreach你可以得到單個文件並上傳它,像這樣
$count = count($FILES['image']['name']);
for($i = 0 ; $i < $count ; $i++){
$file = $FILES['image']['name'][$i];
}
我希望它有所幫助。
檢查此代碼並根據需要進行更改
$arr = array();
$arr = $_FILES['image']['name'];
for($i = 0; $i < count($arr) ; $i++)
{
$file_name = $_FILES['image']['name'][$i];
$file_size = $_FILES['image']['size'][$i];
$file_tmp = $_FILES['image']['tmp_name'][$i];
$file_type = $_FILES['image']['type'][$i];
$responce = move_uploaded_file($file_tmp, "orders/".$file_name);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.