以下是PHP+AJAX上傳一個檔案的程式
想改成上傳多檔
請問應該怎麼改寫呢
感謝幫忙解答
html
<form action="uploadFile.php" method="POST" enctype="multipart/formdata" id="myForm">
<h1>Upload Files-1</h1>
<input type="file" name="myFile" id="myFile" multiple>
<div id="resultArea"></div>
</form>
javascript
$(document).ready(function(){
$("#myFile").on("change", function(){
var input = $(this);
var inputLength = input[0].files.length; //No of files selected
var file;
var formData = new FormData();
for (var i = 0; i < inputLength; i++) {
file = input[0].files[i];
formData.append( 'myFile[]', file);
}
//send POST request to uploadFile.php
$.ajax({
url: "uploadFile.php",
type: "POST",
data: formData,
processData: false,
contentType: false,
beforeSend: function(){
$(".loading").show();
}
}).done(function( data ) {
$(".loading").hide();
$("#resultArea").append(data);
input.val('');
});
});
});
uploadFile.php
<?php
foreach ($_FILES["myFile"]["error"] as $key => $error) {
if($error == 0){
$name = $_FILES["myFile"]["name"][$key];
$uploadPath = "uploads/" . $name;
if(move_uploaded_file( $_FILES["myFile"]["tmp_name"][$key], $uploadPath)){
echo "<a href='".$uploadPath."' target='_BLANK'>".$name."</a><br>";
}
}
}
?>