jQuery Ajax Upload Multiple Images using PHP

Today, We want to share with you jQuery Ajax Upload Multiple Images using PHP.In this post we will show you how to insert image in database using ajax in php, hear for ajax multiple file upload form using jquery demo we will give you demo and example for implement.In this post, we will learn about multiple image upload with preview and delete jquery php with an example.

jQuery Ajax Upload Multiple Images using PHP

There are the Following The simple About jQuery Ajax Upload Multiple Images using PHP Full Information With Example and source code.

Read Also:  jQuery Ajax File Upload images using PHP MySQLi

As I will cover this Post with live Working example to develop upload multiple images in php with preview, so the multiple image upload in php with database for this example is following below.

jQuery Multiple Files Upload Form

JavaScript Source Code:

<script>
$(document).ready(function(){
    $('#uploadForm').ajaxForm({
        target:'#picturesPreview',
        beforeSubmit:function(){
            $('#file_status').html('<img src="file_loading_uploading.gif"/>');
        },
        success:function(){
            $('#pictures').val('');
            $('#file_status').html('');
        },
        error:function(){
            $('#file_status').html('More Images upload failed, please try again.');
        }
    });
});
</script>

HTML Source Code:

<!-- pictures upload form -->
<form method="post" id="uploadForm" enctype="multipart/form-data" action="upload.php">
    <label>Choose Images</label>
    <input type="file" name="pictures[]" id="pictures" multiple >
    <input type="submit" name="submit" value="UPLOAD"/>
</form>

<!-- display upload status -->
<div id="uploadStatus"></div>

Uploaded Images Preview

<!-- gallery view of uploaded pictures --> 
<div class="gallery" id="picturesPreview"></div>

Upload Multiple Images with PHP

upload.php

Read Also:  jQuery AJAX Multiple Files Upload using PHP

<?php
if(isset($_POST['submit'])){
    // PHP File upload configuration
    $targetDir = "uploads/";
    $allowTypes = array('jpg','png','jpeg','gif');
    
    $pictures_arr = array();
    foreach($_FILES['pictures']['name'] as $key=>$val){
        $image_name = $_FILES['pictures']['name'][$key];
        $tmp_name   = $_FILES['pictures']['tmp_name'][$key];
        $size       = $_FILES['pictures']['size'][$key];
        $type       = $_FILES['pictures']['type'][$key];
        $error      = $_FILES['pictures']['error'][$key];
        
        // File upload path
        $fileName = basename($_FILES['pictures']['name'][$key]);
        $live_imgPath = $targetDir . $fileName;
        
        // Check whether file type is valid
        $fileType = pathinfo($live_imgPath,PATHINFO_EXTENSION);
        if(in_array($fileType, $allowTypes)){    
            // Store pictures on the server
            if(move_uploaded_file($_FILES['pictures']['tmp_name'][$key],$live_imgPath)){
                $pictures_arr[] = $live_imgPath;
            }
        }
    }
    
    // Generate powerfule simple all the Display view of the pictures
    if(!empty($pictures_arr)){ ?>
        <ul>
        <?php foreach($pictures_arr as $image_src){ ?>
            <li><img src="<?php echo $image_src; ?>" alt=""></li>
        <?php } ?>
        </ul>
<?php }
}
?>

PHP upload the pictures as well render the preview

Read Also:  crud operations in php

<?php
    $pictures_arr = array();
    foreach($_FILES['pictures']['name'] as $key=>$val){
        //preview pictures without stored
        $picture_data_info = getpicturesize($_FILES['pictures']['tmp_name'][$key]);
        $pictures_arr[] = "data:" . $picture_data_info["mime"] . ";base64," . base64_encode(file_get_contents($_FILES['pictures']['tmp_name'][$key]));
    }
?>

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about jQuery Ajax Upload Multiple Images using PHP.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Ajax Image Upload using PHP and jQuery
  2. Read Also:  PHP Get country city location details
  3. Read Also:  jQuery AJAX file Image upload PHP Example
  4. Read Also:  json to array php
  5. Read Also:  Multiple Image upload preview remove option
  6. Read Also:  jQuery calculate sum of values in all text fields
  7. Read Also:  how to hide div when click outside using jquery?
  8. Read Also:  PHP Laravel Interview Questions and Answers
  9. Read Also:  how to print multidimensional array in php?
  10. Read Also:  jquery multiple Image Upload with preview and Delete using PHP
CLOSEX