PHP - Drag And Drop Image Download

In this tutorial we will create a Drag and Drop Image Download using PHP. This code can move the images via drag and drop function to allow the user to download the image directly. It is easier and fast to upload file to the MySQLi database. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.

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 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_download, 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_download");
  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 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="https://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 - Drag And Drop Image Download</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form method="POST" action="upload.php" enctype="multipart/form-data">
  18. <div class="form-inline">
  19. <input type="file" class="form-control" name="image" required="required"/>
  20. <button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button>
  21. </div>
  22. </form>
  23. <br />
  24. <div class="col-md-4" style="border:1px SOLID #ccc;" ondrop="drop(event)" ondragover="dragOver(event)">
  25. <img src="images/download.png" height="200"/>
  26. </div>
  27. <div class="col-md-8" style="padding:10px;">
  28. <?php
  29. require 'conn.php';
  30.  
  31. $query = mysqli_query($conn, "SELECT * FROM `image`") or die(mysqli_error());
  32. while($fetch = mysqli_fetch_array($query)){
  33. ?>
  34. <img src="<?php echo $fetch['location']?>" id="<?php echo $fetch['img_id']?>" height="100" width="100" style="float:left; margin:10px;" draggable="true" ondragstart="drag(event)"/>
  35. <?php
  36. }
  37. ?>
  38. </div>
  39. </div>
  40. <script src="js/script.js"></script>
  41. </body>
  42. </html>

Creating PHP Query

This code contains the php query of the application. This code will upload 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.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['upload'])){
  5. $image_name = $_FILES['image']['name'];
  6. $image_temp = $_FILES['image']['tmp_name'];
  7. $allowed_ext = array("jpeg", "jpg", "gif", "png");
  8. $exp = explode(".", $image_name);
  9. $ext = end($exp);
  10. $name = date("Y-m-d h-i-s").".".$ext;
  11. $path = "uploads/".$name;
  12. if(in_array($ext, $allowed_ext)){
  13. if(move_uploaded_file($image_temp, $path)){
  14. mysqli_query($conn, "INSERT INTO `image` VALUES('', '$name', '$path')") or die(mysqli_error());
  15. header("location: index.php");
  16. }
  17. }else{
  18. echo "<script>alert('Invalid image format')</script>";
  19. }
  20. }
  21. ?>

Creating the Main Function

This code contains the main function of the application. This code will automatically download the file by dragging the image file to the drop zone. To make this just copy and write these block of codes below inside the text editor, then save it as shown below. download.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_REQUEST['image_id'])){
  5. $image = $_REQUEST['image_id'];
  6. $query = mysqli_query($conn, "SELECT * FROM `image` WHERE `img_id` = '$image'") or die(mysqli_error());
  7. $fetch = mysqli_fetch_array($query);
  8.  
  9. header("Content-Disposition: attachment; filename=".$fetch['image']);
  10. header("Content-Type: application/octet-stream;");
  11. readfile("uploads/".$fetch['image']);
  12. }
  13. ?>
script.js Note: Make sure you save this file inside the js directory.
  1. function dragOver(e){
  2. e.preventDefault();
  3. }
  4.  
  5. function drop(e){
  6. e.preventDefault();
  7. var data = e.dataTransfer.getData("data");
  8.  
  9. window.location = "download.php?image_id="+data;
  10. }
  11.  
  12. function drag(e){
  13. e.dataTransfer.setData("data", e.target.id);
  14. }
There you have it we successfully created Drag And Drop Image Download using PHP. 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