PHP - Simple Multiple Image Upload Using Ajax

In this tutorial we will create a Simple Multiple Image Upload Using Ajax. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. Using Ajax, data could then be passed between the browser and the server, using the XMLHttpRequest API, without having to reload the web page. So Let's do the coding...

Before we get started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_upload, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $conn = new mysqli('localhost', 'root', '', 'db_upload');
  3.        
  4.         if(!$conn){
  5.                 die("Error: Can't connect to database");
  6.         }
  7.        
  8. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="http://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Simple Multiple Image Upload using Ajax</h3>
  16.                 <hr style="boreder-top:1px dotted #ccc;"/>
  17.                 <form id="upload" action="upload.php" method="POST">
  18.                         <div class="form-inline">
  19.                                 <input class="form-control" type="file" name="images[]" multiple="multiple"/>
  20.                                 <button class="btn btn-primary">Upload</button>
  21.                         </div>
  22.                 </form>
  23.         </div>
  24. </body>
  25. <script src="js/jquery-3.2.1.min.js"></script>
  26. <script src="js/bootstrap.js"></script>
  27. <script src="js/script.js"></script>
  28. </html>

Creating PHP Query

This code contains the php query of the application.This code will receive the ajax request then store the data to the database. To do that just copy and write this block of codes inside the text editor, then save it as upload.php.
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if(is_array($_FILES)){
  5.                
  6.                 foreach($_FILES['images']['name'] as $key => $value){
  7.                         $file = explode(".", $_FILES['images']['name'][$key]);
  8.                         $ext = array("png", "gif", "jpg", "jpeg");
  9.                        
  10.                         if(in_array($file[1], $ext)){
  11.                                 $file_name = $file[0].'.'.$file[1];
  12.                                 $source = $_FILES['images']['tmp_name'][$key];
  13.                                 $location = "upload/".$file_name;
  14.                                
  15.                                 if(move_uploaded_file($source, $location)){
  16.                                         $conn->query("INSERT INTO `image` VALUES('', '$file_name', '$location')");
  17.                                 }
  18.                         }
  19.                 }
  20.         }
  21. ?>

Creating The Ajax Script

This is where the code that uses ajax request been used. This code will get the input file data the will send ajax request to the php database server. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(document).ready(function(){
  2.         $('#upload').on('submit', function(e){
  3.                 e.preventDefault();
  4.                
  5.                 $.ajax({
  6.                         url: 'upload.php',
  7.                         type: 'POST',
  8.                         data: new FormData(this),
  9.                         contentType: false,
  10.                         processData: false,
  11.                         success: function(){
  12.                                 $('#upload')[0].reset();
  13.                                 alert("Successfully Uploaded");
  14.                         }
  15.                 });
  16.         });
  17. });
There you have it we successfully created Simple Multiple Image Upload Using Ajax. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment