PHP - Drag And Drop Image Upload Using jQuery

In this tutorial we will create a Drag And Drop Image Upload Using jQuery. 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. So Let's do the coding...

Getting 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_drag, 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 = mysqli_connect('localhost', 'root', '', 'db_drag');
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database!");
  6.         }
  7. ?>

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 shown below.
  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.                 <style>
  7.                         #drop-zone{
  8.                                 margin-top:20px;
  9.                                 height:200px;
  10.                                 background-color:white;
  11.                                 border:3px dashed grey;
  12.                         }
  13.                         .text{
  14.                                 margin-top:70px;
  15.                                 color:grey;
  16.                                 font-size:25px;
  17.                                 font-weight:bold;
  18.                         }
  19.                         #drop-zone img{
  20.                                 max-width:200px;
  21.                         }
  22.                 </style>
  23.         </head>
  24. <body>
  25.         <nav class="navbar navbar-default">
  26.                 <div class="container-fluid">
  27.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  28.                 </div>
  29.         </nav>
  30.         <div class="col-md-3"></div>
  31.         <div class="col-md-6 well">
  32.                 <h3 class="text-primary">PHP - Drag And Drop Image Upload Using jQuery</h3>
  33.                 <hr style="border-top:1px dotted #ccc;"/>
  34.                 <div id="col-md-12" style="text-align:center;">
  35.                         <input type="file">
  36.                         <div id="drop-zone">
  37.                                 <h3 class="text">Drag and Drop Images Here</h3>
  38.                         </div>
  39.                 </div>
  40.         </div>
  41. <script src="js/jquery-3.2.1.min.js"></script>
  42. <script src="js/script.js"></script>
  43. </body>
  44. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the image file to the database server. To do that just copy and write this block of codes inside the text editor, then save it as upload_image.php
  1. <?php
  2.         require_once 'conn.php';
  3.         date_default_timezone_set('Asia/Manila');
  4.        
  5.         if($_FILES){
  6.                 $file_name = $_FILES['image']['name'];
  7.                 $file_temp = $_FILES['image']['tmp_name'];
  8.                 $file_size = $_FILES['image']['size'];
  9.                
  10.                 if($file_size < 2000000){
  11.                         $file = explode('.', $file_name);
  12.                         $end = end($file);
  13.                         $allowed_ext = array('jpg', 'jpeg', 'png');
  14.                        
  15.                         if(in_array($end, $allowed_ext)){
  16.                                 $name = date("Ymd").time();
  17.                                 $location = 'upload/'.$name.".".$end;
  18.                                 if(move_uploaded_file($file_temp, $location)){
  19.                                         mysqli_query($conn, "INSERT INTO `image` VALUES('', '$name', '$location')") or die(mysqli_error());
  20.                                         echo "success";
  21.                                 }
  22.                         }else{
  23.                                 echo "error1";
  24.                         }
  25.                 }else{
  26.                         echo "error2";
  27.                 }
  28.         }
  29.        
  30.  
  31. ?>

Creating the Main Function

This code contains the main function of the application. This code can drag the image to the drop zone, after dropping by it will sent the image data to the php script via ajax request. To make this just copy and write these block of codes below inside the text editor, then save it as script.js inside the js folder.
  1. $(document).ready(function(){
  2.         $("#drop-zone").on('dragenter', function (e){
  3.                 e.preventDefault();
  4.                 $(this).css('background', '#ccc');
  5.         });
  6.  
  7.         $("#drop-zone").on('dragover', function (e){
  8.                 e.preventDefault();
  9.         });
  10.  
  11.         $("#drop-zone").on('drop', function (e){
  12.                 $(this).css('background', '#fff');
  13.                 e.preventDefault();
  14.                 var image = e.originalEvent.dataTransfer.files;
  15.                 uploadimage(image);
  16.         });
  17. });
  18.  
  19. function uploadimage(image) {
  20.         var formData = new FormData();
  21.         formData.append('image', image[0]);
  22.         $.ajax({
  23.                 url: "upload_image.php",
  24.                 type: "POST",
  25.                 data: formData,
  26.                 contentType:false,
  27.                 cache: false,
  28.                 processData: false,
  29.                 success: function(data){
  30.                         if(data == "success"){
  31.                                 alert('Image Uploaded');
  32.                         }else if(data == "error1"){
  33.                                 alert('Wrong Image format');
  34.                         }else if(data == "error2"){
  35.                                 alert('File too large to upload');
  36.                         }
  37.                 }
  38.         });
  39. }
There you have it we successfully created a Drag And Drop Image Upload Using jQuery. 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