PHP Dynamically Add Remove Images Upload using Ajax

Today, We want to share with you PHP Dynamically Add Remove Images Upload using JQuery Ajax.In this post we will show you preview and remove image before upload jquery, hear for multiple image upload with preview and delete jquery php we will give you demo and example for implement.In this post, we will learn about jquery multiple image upload with preview and delete demo with an example.

PHP Dynamically Add Remove Images Upload using JQuery Ajax

There are the Following The simple About PHP Dynamically Add Remove Images Upload using JQuery Ajax Full Information With Example and source code.

As I will cover this Post with live Working example to develop delete image from folder using jquery, so the ajax delete image php for this example is following below.

Add Remove image upload with preview Dynamically using jQuery

multiple image upload with preview and delete option using jquery. With the use of jQuery AJAX, We can step by step upload server side using PHP the selected All the file to your server and display all the files preview of it. If We can like to dynamic add a remove(delete) button to delete the file on servser side without any page load then We again required to use jquery AJAX request call to delete or remove(unlink) the file from the server.

Read Also:  JavaScript Remove Object from Array

Step 1: HTML Interface


<!doctype html>
        <title>jquery multiple image upload with preview and delete demo</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <style type='text/css'>
        margin: 10px;
        width: 100px;
        float: left;
        margin-right: 5px;
        border: 2px solid blue;
        border-radius: 5px;
        padding: 6px;

        /* Remove or Delete Images */
        .preview span{
        border: 2px solid green;
        display: inline-block;
        width: 98%; 
        text-align: center;
        color: green;
        .preview span:hover{
        cursor: pointer;
    <body >
        <h2>how to insert image in database using ajax in php</h2>
        <form method="post" action="" enctype="multipart/form-data" id="myform">
            <input type="file" id="file" name="file" />
            <input type="button" class="button" value="Upload" id="fileimg_uploads">
        <!-- Image element blackcard -->
        <div class="blackcard"></div>
		<a href="" target="_blank" alt="pakainfo" title="pakainfo">Free Download Example -</a>

        <!-- Script -->
        <script src='' type='text/javascript'></script>
        <script type='text/javascript'>

                // Upload

                    var fimgdata = new FormData();
                    var files = $('#file')[0].files[0];

                    //jQuery AJAX request (post Methods)
                        url: 'addremove.php',
                        type: 'post',
                        data: fimgdata,
                        contentType: false,
                        processData: false,
                        success: function(response){
                            if(response != 0){
                                var total = $('.blackcard .preview').length;
                                total = Number(total) + 1;

                                // Show image preview with Delete button
                                $('.blackcard').append("<div class='preview' id='preview_"+total+"' ><img src='"+response+"' width='100' height='100'><span class='delete' id='delete_"+total+"'>Delete</span></div>");
                                alert('file not uploaded');

                // Remove blackcard file
                $('.blackcard').on('click','.preview .delete',function(){
                    var id =;
                    var split_id = id.split('_');
                    var num = split_id[1];

                     // Get image source
                    var imgElement_src = $( '#preview_'+num+' img' ).attr("src");
                    // AJAX request
                       url: 'addremove.php',
                       type: 'post',
                       data: {path: imgElement_src,storedata: 2},
                       success: function(response){
                            // Remove <div >
                            if(response == 1){



Setp 2 : Upload FILE On Server Side



$get_files_data = $_POST['storedata'];

// Upload file
if($get_files_data == 1){

	$filename = $_FILES['file']['name'];
	/* destination_path */
	$destination_path = "upload/".$filename;
	$isUploded = 1;
	$flImgTypeCheck = pathinfo($destination_path,PATHINFO_EXTENSION);

	// Check FILE or image format
	if($flImgTypeCheck != "jpg" && $flImgTypeCheck != "png" && $flImgTypeCheck != "jpeg"
	 && $flImgTypeCheck != "gif" ) {
	 	$isUploded = 0;

	if($isUploded == 0){
	 	echo 0;
	 /* Upload with Move files or Images */
	 		echo $destination_path;
	 		echo 0;

// Remove or unlink file
if($get_files_data == 2){
	$get_path = $_POST['path'];
	$res_data = 0;

	// Check file exist or not
	if( file_exists($get_path) ){

	// Remove file 

	// Set res_data
	 $res_data = 1;

	// Set res_data
	 $res_data = 0;

	// Return res_data
	echo $res_data;

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  jquery multiple Image Upload with preview and Delete using PHP


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

Free Live Chat for Any Issue

I hope you get an idea about PHP Dynamically Add Remove Images Upload using JQuery Ajax.
I would like to have feedback on my 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.