Jquery file :
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
Uploads.php contents :
<?php
if (isset($_FILES[files]) && !empty($_FILES[files])) {
$no_files = count($_FILES["files"][name]);
for ($i = 0; $i < $no_files; $i++) {
if ($_FILES["files"]["error"][$i] > 0) {
echo "Error: " . $_FILES["files"]["error"][$i] . "<br>";
} else {
if (file_exists(uploads/ . $_FILES["files"]["name"][$i])) {
echo File already exists : uploads/ . $_FILES["files"]["name"][$i];
} else {
move_uploaded_file($_FILES["files"]["tmp_name"][$i], uploads/ . $_FILES["files"]["name"][$i]);
echo File successfully uploaded : uploads/ . $_FILES["files"]["name"][$i] . ;
}
}
}
} else {
echo Please choose at least one file;
};
?>
Html content :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>File upload using PHP, jQuery and AJAX</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function (e) {
$(#upload).on(click, function () {
var form_data = new FormData();
var ins = document.getElementById(multiFiles).files.length;
for (var x = 0; x < ins; x++) {
form_data.append("files[]", document.getElementById(multiFiles).files[x]);
}
$.ajax({
url: uploads.php, // point to server-side PHP script
dataType: text, // what to expect back from the PHP script
cache: false,
contentType: false,
processData: false,
data: form_data,
type: post,
success: function (response) {
$(#msg).html(response); // display success response from the PHP script
},
error: function (response) {
$(#msg).html(response); // display error response from the PHP script
}
});
});
});
</script>
</head>
<body>
<p id="msg"></p>
<input type="file" id="multiFiles" name="files[]" multiple="multiple"/>
<button id="upload">Upload</button>
</body>
</html>
İlk Yorumu Sen Yap