AJAX, JQuery 및 PHP를 사용하여 한 번에 여러 이미지를 한 폴더에 업로드하려고합니다. 코드는 단일 파일 업로드를 위해 실행 중이지만 여러 이미지 업로드를 위해 실행되지 않습니다. 하나의 이미지를 루프없이 업로드 할 경우 잘 작동하지만 루프의 경우에는 작동하지 않습니다.ajax php 및 jquery를 사용하여 여러 이미지를 폴더에 업로드하는 방법
다음 코드를 사용하고 있습니다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload Iamge</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="jquery-1.12.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#but_upload").click(function(){
var fd = new FormData();
//following code is working fine in for single image upload
// var files = $('#file')[0].files[0];
// fd.append('file',files);
//this code not working for multiple image upload
var names = [];
for (var i = 0; i < $('#file').get(0).files.length; ++i) {
names.push($('#file').get(0).files[i].name);
}
fd.append('file[]',names);
/*var ins = document.getElementById('file').files.length;
for (var x = 0; x <ins; x++) {
fd.append("file", document.getElementById('file').files[x]);
}*/
$.ajax({
url:'upload.php',
type:'post',
data:fd,
contentType: false,
processData: false,
success:function(response){
if(response != 0){
$("#img").attr("src",response);
}
},
error:function(response){
alert('error : ' + JSON.stringify(response));
}
});
});
});
</script>
</head>
//HTML Part
<body>
<div class="container">
<h1>AJAX File upload</h1>
<form method="post" action="" id="myform">
<div>
<img src="" id="img" width="100" height="100">
</div>
<div>
<input type="file" id="file" name="file" multiple="multiple" />
<input type="button" class="button" value="Upload"
id="but_upload">
</div>
</form>
</div>
</body>
</html>
//PHP Code
<?php
/* Getting file name */
echo "<script>alert('yes');</script>";
//without loop working fine
$count = count($_FILES['file']['name']);
for ($i = 0; $i < $count; $i++) {
$filename = $_FILES['file']['name'][$i];
/* Location */
$location = "upload/".$filename;
/* Upload file */
if(move_uploaded_file($_FILES['file']['tmp_name'],$location)){
echo $location;
} else {
echo 0;
}
}
?>
도 $ 추가 i가 작동하지 않는 그 후 특정 파일의
index
를 추가해야합니다! –거기에 오류가 있거나 뭔가 error_reporting (-1); ini_set ("display_errors", true) PHP 페이지에서 오류를 콘솔에 체크인하십시오 –
오류가 없습니다.이 예제를 실행 해보십시오. –