Image upload functionality is commonly used in web applications and it can be easily implemented using PHP. But if you want to implement image upload functionality without page refresh, jQuery and Ajax are required with PHP. Upload image without page refresh provides a user-friendly interface for the web application. With Ajax file upload functionality the user can upload images without reloading or refreshing the current page.
Generally, the file input field uploads a single image at a time. But in some cases, your web application requires allowing the user to upload multiple images at the same time. In this tutorial, we will show you how to upload multiple images without page refresh using jQuery, Ajax, and PHP. Ajax multiple image upload allows the user to select multiple image files at once and upload all the images to the server in a single click.
The example code helps you to upload multiple images at once without page refresh using jQuery, Ajax, and PHP. It simplifies the multiple image upload without refreshing the page. There are two ways to display the preview of the uploaded image. You can show the uploaded images with or without stored in a directory of the server.
We will use the HTML to create a file upload form and jQuery to post the images to the server-side for upload.
JavaScript Code:
The jQuery Form Plugin will be used to post file data via Ajax request. It allows you to upgrade the HTML file upload form to use AJAX. So, include the jQuery library and jQuery Form Plugin to submit the form with files for uploading using jQuery and Ajax.
<!-- jQuery library -->
<script type="text/javascript" src="js/jquery.min.js"></script>
<!-- jQuery Form Plugin -->
<script type="text/javascript" src="js/jquery.form.js"></script>
The ajaxForm method of jQuery Form Plugin prepares the HTML form for AJAX submission. Any $.ajax
options can be passed in ajaxForm() function.
target
– Specify the element(s) to update with the server response.beforeSubmit
– A callback function that is invoked before form submission.success
– A callback function that is invoked after the response has been returned from the server.error
– A callback function that is invoked if an error occurred.<script>
$(document).ready(function(){
$('#uploadForm').ajaxForm({
target:'#imagesPreview',
beforeSubmit:function(){
$('#uploadStatus').html('<img src="css/uploading.gif"/>');
},
success:function(){
$('#images').val('');
$('#uploadStatus').html('');
},
error:function(){
$('#uploadStatus').html('<p>Upload failed! Please try again.</p>');
}
});
});
</script>
HTML Code:
Create an HTML form with an input field to allow the user to select multiple images they want to upload.
method="post"
and enctype="multipart/form-data"
type="file"
and multiple attributes.<!-- images upload form --> <form method="post" id="uploadForm" enctype="multipart/form-data" action="upload.php"> <label>Choose Images</label> <input type="file" name="images[]" id="images" multiple > <input type="submit" name="submit" value="UPLOAD"/> </form> <!-- display upload status --> <div id="uploadStatus"></div>
Uploaded Images Preview: Create div with target ID which is defined in ajaxForm
of Form Plugin API. The uploaded images will be displayed in this div.
<!-- gallery view of uploaded images --> <div class="gallery" id="imagesPreview"></div>
Upload Multiple Files and Images in CodeIgniter
The upload.php
file process the multiple image upload using PHP and render the uploaded images in a gallery view.
<?php
if(isset($_POST['submit'])){
// File upload configuration
$targetDir = "uploads/";
$allowTypes = array('jpg','png','jpeg','gif');
$images_arr = array();
foreach($_FILES['images']['name'] as $key=>$val){
$image_name = $_FILES['images']['name'][$key];
$tmp_name = $_FILES['images']['tmp_name'][$key];
$size = $_FILES['images']['size'][$key];
$type = $_FILES['images']['type'][$key];
$error = $_FILES['images']['error'][$key];
// File upload path
$fileName = basename($_FILES['images']['name'][$key]);
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
// Store images on the server
if(move_uploaded_file($_FILES['images']['tmp_name'][$key], $targetFilePath)){
$images_arr[] = $targetFilePath;
}
}
}
// Generate gallery view of the images
if(!empty($images_arr)){ ?>
<ul>
<?php foreach($images_arr as $image_src){ ?>
<li><img src="<?php echo $image_src; ?>" alt=""></li>
<?php } ?>
</ul>
<?php }
}
?>
Create Dynamic Image Gallery with jQuery, PHP & MySQL
You can upload the images and render the preview of the images without stored on the server.
<?php
$images_arr = array();
foreach($_FILES['images']['name'] as $key=>$val){
//display images without stored
$extra_info = getimagesize($_FILES['images']['tmp_name'][$key]);
$images_arr[] = "data:" . $extra_info["mime"] . ";base64," . base64_encode(file_get_contents($_FILES['images']['tmp_name'][$key]));
}
?>
Here we have tried to make it simple to upload multiple images without page refresh using jQuery, Ajax, and PHP. You can also upload multiple images without any jQuery plugin using PHP. If you’re looking the advanced uploading feature in your web application, implement Drag and drop file upload using Dropzone JS and PHP.
Upload Image and Create Thumbnail using PHP
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Great, it works..
How to show upload progress (by process % or process bar)?
Hello please can you help me with multiple images uploading like olx.com
Hello please can you help me with registration images uploading like olx.com
how to upload image and pdf files with two different input type and two different progress bar in ajax and php
Hello, very nice script. How can i style the button in the modal?
Great…
Very nice.. but user registration along with image upload?…it’s a challenge.Regards
Thank you so much. Thanks for sharing.
HI
Would tell me how to show multiple image below the upload box .
I always like to reference tutorials like this. Thanks for taking the time to share. Really well done.
Excellent Code.. 100% working
I’d like to request the ‘remove’ option from displayed photos, before they are uploaded. Any idea when/where we could read your code on that feature? Thanks for the info!
Is possible add progress bar?
Great script. But: Is there a reason why the script is limited to 20 files. I can choose as many as I want. But there are only 20 uploads?! Is it possible tu upgrade?
@FTE Yes, it is possible. You should need to change the max_file_uploads variable value in php.ini file. Please follow this guide – http://www.codexworld.com/how-to-guides/increase-files-upload-limit-in-php/
@FTE Yes, it is possible. You should need to change the max_file_uploads variable value in php.ini file. Please follow this guide – http://www.codexworld.com/how-to-guides/increase-files-upload-limit-in-php/
@CODEX WORLD
i don’t know how to say thanks to you…
i’m a beginner of PHP & i found to many interesting articles here 🙂 🙂
all codes are working perfectly & A BIG THANKS FOR DIRECT DOWNLOAD OPTION FOR CODE 🙂 🙂
<3 u @Codexworld
how i download this tutorial
@Ali Download this tutorial from the above Download link. Please let us know, if need further help.
but how create photo album user interface title and description
Wonderful! Exactly what I was looking for, could you please tell how to insert them into database and retrieve them in the form of a single post, I am having trouble doing so, Thank You
Sir please tell me how to create boostrap thumbnails gallery with php pagination…
Hi! it seems that the downloadable version of this does not append to the first uploaded batch of images, it replaces them and display only the newly selected images unlike the demo which appends the newly uploaded to the already uploaded images. thanks. Can we know how to do that?
hai it was nice post ..but i need some help in it .how can we create image album ..when we upload multi images it should save like album ..can u say how can we do it ..thanks in advanced i hope u ll get me out from this
really nice code but i faced one issue i download the script and run into localhost when i upload a one image its show correctly but when i upload second image so first image is replace.i dont want like that,your demo is work correctly i want similar like that please help me
hiii
thanx
it is possible to add the file up loader progress-bar in that code…
Hi, this tutorial is exactaly what i need.
I have a problem, when i try to upload 5 o more images, show a error message:
Notice: Undefined index: image_form_submit in C:\wamp\www\upload\upload.php on line 2
Someone has the same problem? help me please.
You can avoid this error easily. Just remove the below condition into the
upload.php
file.Great, it works..
is it anyway to rename the image file before it upload?
Yes possible. Just modify the
$target_file
variable value like the following.time()
is used for adding timestamp to beginning of the image name. You can replace the timestamp with your desire file name.If you want any other modification, please comment here. We are happy to help our users.
great work thank You!!
That’s great code. Thank you.
Could you please add remove option into code?
Thanks Serkan.
We will release the new version with the remove option shortly. Please subscribe our newsletter for receive the new version of this post at your inbox.
Great, great..helps a lot!
Great tutorial bro. I like it.