Upload multiple images using jQuery, Ajax and PHP


We’ve received many requests from followers for publishing the tutorial on How to upload multiple images without refreshing the page. We’ll try to simplify the multiple images upload without refreshing the page. In this simple tutorial, we’ll discuss multiple images uploads using jQuery, Ajax and PHP. Using our script, you can upload multiple images at once without page refresh using jQuery, Ajax and PHP.

We’ll show you two ways to display images after upload. Once the images are uploaded to the server via jQuery, you can display images without stored in a folder or stored in a folder. This script is very short, easy and useful which can easily be implemented in your web projects.

Read the following step-by-step guide to understanding the whole process of Upload multiple images using jQuery, Ajax and PHP. Also, you can see the live demo from the Demo link and download the source code from the Download link at the end of the tutorial.


index.html File

index.html file contains some JavaScript and HTML codes. All codes are given below.

JavaScript Code:
At first include the two external JavaScript library files, jquery.min.js and jquery.form.js. jquery.min.js is the main jQuery library and another jquery.form.js file is used to submit the form with files for uploading using jQuery Ajax.

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>

Now write some JavaScript code for form submission and displaying the uploaded images.

<script type="text/javascript">
            //display the uploaded images


Input file field name is defined as an array for accept multiple file name and also used multiple attributes for multiple upload support.

<form method="post" name="multiple_upload_form" id="multiple_upload_form" enctype="multipart/form-data" action="upload.php">
    <input type="hidden" name="image_form_submit" value="1"/>
    <label>Choose Image</label>
    <input type="file" name="images[]" id="images" multiple >
    <div class="uploading none">
        <img src="uploading.gif" alt="uploading......"/>

Create div with target id which is defined at the above JavaScript code. The uploaded images would be displayed at this div. Also you can change this div id, but remember that the JavaScript target option value (target:'#images_preview') will be same with this div id.

<div id="images_preview"></div>

upload.php file:

upload.php file contain some PHP codes for process the uploading and generates the images view. There are two way to upload images and display the images.

 One Way is to create a folder named uploads/. Upload the images and stored in the folder.

= array();
$_FILES['images']['name'] as $key=>$val){
//upload and stored images
$target_dir "uploads/";
$target_file $target_dir.$_FILES['images']['name'][$key];
$images_arr[] = $target_file;

 You can upload the images and display the images without stored in the folder.

= array();
$_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]));

Generate images view
After uploading the images we need to generate the view. This view will display at the target div.

$images_arr as $image_src){ ?> <ul> <li > <img src="<?php echo $image_src; ?>" alt=""> </li> </ul> <?php }

Related Articles

Comments 0