Ron Ron - 1 month ago 8
jQuery Question

How to handle multiple image upload one by one in php

we have an html form with

<input type="file" name="u_file" class="u_class" id="u_id">
. When user upload an image then it will show in the below div before submitting the form .

<div class="preview-div">


</div>.


That is when user upload an image then that time we append image to the div by jquery .

$("#u_id").change(function(event){

$(".preview-div").append(' <img style="" class="img-class" src="'+URL.createObjectURL(event.target.files[0])+'">');
etc.....

});


when submit form then it will check

if($_FILES['u_file']['name'] !=""){

$uploadedfile = $_FILES['user_file'];
$image_name=$_FILES['u_file']['name'];

move_imageto_dir();
etc....

}


here
move_imageto_dir();
is the custom function we written .

But the problem is that it will only checking and moving the last image . How to solve this ?.

That is ($_FILES['u_file'] only contain the latest upload image . What we need to do ?We can't use
<input type='file' multiple>


Thank you .

Answer

Name your file inputs as a array

<input type="file" name="u_file[]" class="u_class" id="u_id">

$_FILES['u_file'] is a array now

foreach ($_FILES['u_file'] as $file) {
$image_name=$file['name'];

move_imageto_dir();
etc....

}