PHP Jquery Ajax Upload Multiple Images with Preview Example

PHP Jquery Ajax Upload Multiple Images with Preview

Today, We want to share with you PHP Jquery Ajax Upload Multiple Images with Preview Example.In this post we will show you multiple image upload in laravel using ajax, hear for multiple image upload in php with database 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.

PHP Jquery Ajax Upload Multiple Images with Preview Example

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

As I will cover this Post with live Working example to develop how to upload multiple images in php using for loop, so the some image upload using ajax php mysql for this example is following below.

  global variable for all controller and views in Laravel

Step 1 : Javascript Code:

jQuery Source code

<script>
$(document).ready(function(){
    $('#submitForm').ajaxForm({
        target:'#products_imgPrev',
        beforeSubmit:function(){
            $('#status').html('<img src="image.gif"/>');
        },
        success:function(){
            $('#imgs').val('');
            $('#status').html('');
        },
        error:function(){
            $('#status').html('Sorry Your products_img uploading failed, please try again.');
        }
    });
});
</script>

Step 2 : HTML Code:

HTML Source Code:

<form method="post" id="submitForm" enctype="multipart/form-data" action="upload.php">
    <label>Choose products_img</label>
    <input type="file" name="products_img[]" id="imgs" multiple >
    <input type="submit" name="submit" value="UPLOAD"/>
</form>
<div id="status"></div>


<div class="gallery" id="products_imgPrev"></div>

Step 3 : PHP Source Code:

PHP Code: (upload.php)

<?php
if(isset($_POST['submit'])){

    $target_dir = "uploads/";
    $allow_types = array('jpg','png','jpeg','gif');
    
    $products_img_arr = array();
    foreach($_FILES['products_img']['name'] as $key=>$val){
        $image_name = $_FILES['products_img']['name'][$key];
        $tmp_name   = $_FILES['products_img']['tmp_name'][$key];
        $size       = $_FILES['products_img']['size'][$key];
        $type       = $_FILES['products_img']['type'][$key];
        $error      = $_FILES['products_img']['error'][$key];
        
        
        $file_name = basename($_FILES['products_img']['name'][$key]);
        $uploadFileImgDir = $target_dir . $file_name;
        
        
        $file_type = pathinfo($uploadFileImgDir,PATHINFO_EXTENSION);
        if(in_array($file_type, $allow_types)){    
            
            if(move_uploaded_file($_FILES['products_img']['tmp_name'][$key],$uploadFileImgDir)){
                $products_img_arr[] = $uploadFileImgDir;
            }
        }
    }
    
   
    if(!empty($products_img_arr)){ ?>
        <ul>
        <?php foreach($products_img_arr as $image_src){ ?>
            <li><img src="<?php echo $image_src; ?>" alt=""></li>
        <?php } ?>
        </ul>
<?php }
}
?>

Angular 6 CRUD Operations Application Tutorials

Read :

  PHP Class Inheritance call parent constructor

Summary

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

I hope you get an idea about PHP Jquery Ajax Upload Multiple Images with Preview Example.
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.