Upload Multiple Images With Image Preview Using jQuery,Ajax And PHP

Upload Multiple Images With Image Preview Using jQuery,Ajax And PHP

In this Post We Will Explain About is Upload Multiple Images With Image Preview Using jQuery,Ajax And PHP With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to How to Upload multiple images jQuery Ajax using PHP Example

In this post we will show you Best way to implement multiple image upload with preview and delete jquery, hear for Upload Multiple Images using jQuery, PHP & MySQL with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

How to Upload multiple images jQuery Ajax using PHP

In this Example,First of all Add or Inluce External Libs Like as a(jQuery, css etc..), and then create a simple index.php or index.html page.After that crate a simple javascript file like as a index.js or main.js, It is also add your web-application First Header Part to some priorty set.After that Include your relavant CSS Class.

Read Also:  encapsulation in php

How to Upload multiple images jQuery Ajax using PHP

index.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/uploadfilemulti.css" rel="stylesheet">
<!-- Devloped by Pakainfo.com free download examples -->
<script src="js/jquery-1.8.0.min.js"></script>
<script src="js/jquery.fileuploadmulti.min.js"></script>
</head>
<body>
<!-- Devloped by Pakainfo.com free download examples -->
<div id="liveMultipleFileupload">Upload</div>

<div id="data_status"></div>
<script>

$(document).ready(function()
{

var settings = {
	//send url to all images
	url: "upload.php",
	method: "POST",
	allowedTypes:"jpg,png,gif,doc,pdf,zip",
	fileName: "liveuploadFile",
	multiple: true,
	onSuccess:function(files,data,xhr)
	{
		$("#data_status").html("<font color='green'>Upload is success</font>");
        },
        afterUploadAll:function()
        {
                alert("Good Luck all images uploaded!!");
        },
	onError: function(files,data_status,errMsg)
	{		
		$("#data_status").html("<font color='red'>Your Upload is Failed</font>");
	}
}
$("#liveMultipleFileupload").uploadFile(settings);

});
</script>
</body>

upload.php

simple File contains post upload all the files process as well as move all files in uploads Folder.

Read Also:  How to Get the Current URL with VueJS?

<?php
$output_dir = "uploads/";

if(isset($_FILES["liveuploadFile"]))
{
	$results = array();

	$error =$_FILES["liveuploadFile"]["error"];
   {

    	if(!is_array($_FILES["liveuploadFile"]['name'])) //get here single file
    	{
            $get_randomNo   = time();

            $LiveImgName      = str_replace(' ','-',strtolower($_FILES['liveuploadFile']['name']));
            $ImageType      = $_FILES['liveuploadFile']['type']; 

            $LiveImgExt = substr($LiveImgName, strrpos($LiveImgName, '.'));
            $LiveImgExt       = str_replace('.','',$LiveImgExt);
            $LiveImgName      = preg_replace("/\.[^.\s]{3,4}$/", "", $LiveImgName);
            $NewLiveImgName = $LiveImgName.'-'.$get_randomNo.'.'.$LiveImgExt;

       	 	move_uploaded_file($_FILES["liveuploadFile"]["tmp_name"],$output_dir. $NewLiveImgName);
       	 	 //echo "<br> Error: ".$_FILES["liveuploadFile"]["error"];

	       	 	 $results[$fileName]= $output_dir.$NewLiveImgName;
    	}
    	else
    	{
            $fileCount = count($_FILES["liveuploadFile"]['name']);
    		for($i=0; $i < $fileCount; $i++)
    		{
                $get_randomNo   = time();

                $LiveImgName      = str_replace(' ','-',strtolower($_FILES['liveuploadFile']['name'][$i]));
                $ImageType      = $_FILES['liveuploadFile']['type'][$i]; //"image/png", image/jpeg etc.

                $LiveImgExt = substr($LiveImgName, strrpos($LiveImgName, '.'));
                $LiveImgExt       = str_replace('.','',$LiveImgExt);
                $LiveImgName      = preg_replace("/\.[^.\s]{3,4}$/", "", $LiveImgName);
                $NewLiveImgName = $LiveImgName.'-'.$get_randomNo.'.'.$LiveImgExt;

                $results[$NewLiveImgName]= $output_dir.$NewLiveImgName;
    		    move_uploaded_file($_FILES["liveuploadFile"]["tmp_name"][$i],$output_dir.$NewLiveImgName );
    		}
    	}
    }
    echo json_encode($results);

}

?>

First of all check Folder and get some error and then check if create single file coming then simple process separate if all the multiple files handle in php loop.

Read Also:  How to check Laravel version and configuration

$get_randomNo   = time();

$LiveImgName      = str_replace(' ','-',strtolower($_FILES['liveuploadFile']['name'][$i]));
$ImageType      = $_FILES['liveuploadFile']['type'][$i]; //"image/png", image/jpeg etc.

$LiveImgExt       = substr($LiveImgName, strrpos($LiveImgName, '.'));
$LiveImgExt       = str_replace('.','',$LiveImgExt);
$LiveImgName      = preg_replace("/\.[^.\s]{3,4}$/", "", $LiveImgName);
$NewLiveImgName   = $LiveImgName.'-'.$get_randomNo.'.'.$LiveImgExt;

Add simple using PHP UNIX time stamp with create image name to generated unique name.

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Example

I hope you have Got What is How to Upload multiple images jQuery Ajax using PHP And how it works.I would Like to have FeedBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Multiple Image upload preview remove option
  2. Read Also:  PHP and Ajax Asynchronous File Upload using jQuery
  3. Read Also:  PHP Rest API And HTTP CURL methods GET, POST, DELETE, PUT
  4. Read Also:  Upload Multiple Images Preview using PHP
  5. Read Also:  jquery multiple Image Upload with preview and Delete using PHP
  6. Read Also:  Display Image Preview Before Upload Multiple Images using jQuery,Ajax And PHP
  7. Read Also:  Event Watch route change with VueJS
  8. Read Also:  Dynamically Upload Multiple Images Using PHP and jQuery
  9. Read Also:  Simple jQuery Fade Effects Example
  10. Read Also:  Convert CSV File Into JSON using PHP