PHP - Drag And Drop Image Download

Submitted by: 
Visitors have accessed this post 196 times.

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

And, this is the link for the bootstrap that i used for the layout design

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.

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");
  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="<a href="">Sourcecodester</a>
  11. " rel="nofollow">">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Drag And Drop Image Download</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <form method="POST" action="upload.php" enctype="multipart/form-data">
  19. <div class="form-inline">
  20. <input type="file" class="form-control" name="image" required="required"/>
  21. <button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button>
  22. </div>
  23. </form>
  24. <br />
  25. <div class="col-md-4" style="border:1px SOLID #ccc;" ondrop="drop(event)" ondragover="dragOver(event)">
  26. <img src="images/download.png" height="200"/>
  27. </div>
  28. <div class="col-md-8" style="padding:10px;">
  29. <?php
  30. require 'conn.php';
  32. $query = mysqli_query($conn, "SELECT * FROM `image`") or die(mysqli_error());
  33. while($fetch = mysqli_fetch_array($query)){
  34. ?>
  35. <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)"/>
  36. <?php
  37. }
  38. ?>
  39. </div>
  40. </div>
  41. <script src="js/script.js"></script>
  42. </body>
  43. </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';
  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.

  1. <?php
  2. require_once 'conn.php';
  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);
  9. header("Content-Disposition: attachment; filename=".$fetch['image']);
  10. header("Content-Type: application/octet-stream;");
  11. readfile("uploads/".$fetch['image']);
  12. }
  13. ?>

Note: Make sure you save this file inside the js directory.

  1. function dragOver(e){
  2. e.preventDefault();
  3. }
  5. function drop(e){
  6. e.preventDefault();
  7. var data = e.dataTransfer.getData("data");
  9. window.location = "download.php?image_id="+data;
  10. }
  12. function drag(e){
  13. e.dataTransfer.setData("data",;
  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

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.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.