HTML
<form>
<label>Image</label>
<input type="file" name="images" id="images" />
<button id="save">
</form>
JS
$(function(){
var input = document.getElementById("images"),
formdata = false;
formdata = new FormData();
$("#save").click(function() {
var i = 0, len = input.files.length, img, reader, file;
for ( ; i < len; i++ ) {
file = input.files[i];
if (!!file.type.match(/image.*/)) {
if ( window.FileReader ) {
reader = new FileReader();
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("images[]", file);
}
}
}
if (formdata) {
$.ajax({
url: "<?php echo base_url('main/save')?>",
type: "POST",
data: formdata,
processData: false,
contentType: false,
});
}
});
});
PHP
function save() {
foreach ($_FILES["images"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["images"]["name"][$key];
$nama_file = $_FILES['images']['name'][$key];
$namafolder = 'uploads/';
move_uploaded_file( $_FILES["images"]["tmp_name"][$key],$namafolder.'/'.$nama_file);
}
}
}
<form>
<label>Image</label>
<input type="file" name="images" id="images" />
<button id="save">
</form>
JS
$(function(){
var input = document.getElementById("images"),
formdata = false;
formdata = new FormData();
$("#save").click(function() {
var i = 0, len = input.files.length, img, reader, file;
for ( ; i < len; i++ ) {
file = input.files[i];
if (!!file.type.match(/image.*/)) {
if ( window.FileReader ) {
reader = new FileReader();
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("images[]", file);
}
}
}
if (formdata) {
$.ajax({
url: "<?php echo base_url('main/save')?>",
type: "POST",
data: formdata,
processData: false,
contentType: false,
});
}
});
});
PHP
function save() {
foreach ($_FILES["images"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["images"]["name"][$key];
$nama_file = $_FILES['images']['name'][$key];
$namafolder = 'uploads/';
move_uploaded_file( $_FILES["images"]["tmp_name"][$key],$namafolder.'/'.$nama_file);
}
}
}