PHP - Drag And Drop Multiple Files Using jQuery

Submitted by: 
Language: 
Visitors have accessed this post 388 times.

In this tutorial we will create a Drag And Drop Multiple Files 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. The jQuery is a fast, reliable kind of cross-platform javascript library. It is designed to simplify the traditional way of coding in javascript. 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_drop, 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_drop');
  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. .file_zone{
  8. width:300px;
  9. height:300px;
  10. border:2px dashed #ccc;
  11. line-height:300px;
  12. text-align:center;
  13. font-size:28px;
  14. float:left;
  15. }
  16.  
  17. .file_drag{
  18. color:#000;
  19. border-color:#000;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <nav class="navbar navbar-default">
  25. <div class="container-fluid">
  26. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  27. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  28. </a> </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 Multiple Files Using jQuery</h3>
  33. <div class="file_zone">DROP FILES HERE</div>
  34. <div style="border-left:2px solid #000; width:280px;" align="center" id="uploaded_file" class="pull-right">
  35.  
  36. </div>
  37. </div>
  38. </body>
  39. <script src="js/jquery-3.2.1.min.js"></script>
  40. <script src="js/bootstrap.js"></script>
  41. <script src="js/script.js"></script>
  42. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the file to the database server, and also display the data at the same time. To do that just copy and write this block of codes inside the text editor, then save it as shown below.

upload.php

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_FILES['file']['name'][0])){
  5. foreach($_FILES['file']['name'] as $key => $values){
  6.  
  7. $file = explode(".", $values);
  8. $ext = array("png", "gif", "jpg", "jpeg");
  9.  
  10. if(in_array($file[1], $ext)){
  11. $file_name = $file[0].'.'.$file[1];
  12. $tmp_file = $_FILES['file']['tmp_name'][$key];
  13. $location = "upload/".$file_name;
  14.  
  15. if(move_uploaded_file($tmp_file, $location)){
  16. $conn->query("INSERT INTO `upload` VALUES('', '$file_name', '$location')");
  17. }
  18. }
  19. }
  20. }
  21. ?>

data.php

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. $query = $conn->query("SELECT * FROM `upload`");
  5.  
  6. while($fetch = $query->fetch_array()){
  7. echo '<img src="'.$fetch['location'].'" width="250px" height="200px" style="margin-top:10px; margin-bottom:10px;"/>';
  8. }
  9. ?>

Creating jQuery Script

This is where the main function of the application.This code will allow the user to drag image file zone and will send the image data with ajax request. 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. DisplayImage();
  3.  
  4. function DisplayImage(){
  5. $.ajax({
  6. url: 'data.php',
  7. type: 'POST',
  8. data: {res:1},
  9. success: function(data){
  10. $('#uploaded_file').html(data);
  11. }
  12. });
  13. }
  14.  
  15.  
  16. $('.file_zone').on('dragleave', function(){
  17. $(this).removeClass('file_drag');
  18. });
  19.  
  20. $('.file_zone').on('dragover', function(){
  21. $(this).addClass('file_drag');
  22. return false;
  23. });
  24.  
  25.  
  26. $('.file_zone').on('drop', function(e){
  27. e.preventDefault();
  28. $(this).removeClass('file_drag');
  29. var formData = new FormData();
  30. var file_list = e.originalEvent.dataTransfer.files;
  31.  
  32. for(var i=0; i<file_list.length; i++){
  33. formData.append('file[]', file_list[i]);
  34. }
  35.  
  36. $.ajax({
  37. url: 'upload.php',
  38. method: "POST",
  39. data: formData,
  40. contentType: false,
  41. cache: false,
  42. processData: false,
  43. success: function(){
  44. DisplayImage();
  45. }
  46. });
  47. });
  48. });

There you have it we successfully created Drag And Drop Multiple Files 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.