PHP - Dynamically Remove File By Dragging

In this tutorial we will create a Dynamically Remove File By Dragging using PHP. 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 bootstrap that i used for the layout design https://getbootstrap.com/.

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="navabr 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">JavaScript - Autofill Inputs By Dragging</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <center><img src="image/trash.png" height="200" width="200" ondrop="drop(event)" ondragover="dragOver(event)"></center>
  20. </div>
  21. <br style="clear:both;"/><br />
  22. <div class="col-md-2"></div>
  23. <div class="col-md-8" style="border:1px SOLID #ccc; padding:10px;">
  24. <?php
  25. $files = scandir('files/');
  26. foreach ($files as $file){
  27. if($file != '.' && $file !='..'){
  28. $path = "files/".$file;
  29. echo "<div id='".$file."' draggable='true' ondragstart='drag(event)' style='float:left; margin:12px;'><img src='image/image.png' height='30' width='30'/><label>".$file."</label></div>";
  30. }
  31. }
  32. ?>
  33. </div>
  34. </div>
  35. <script src="js/script.js"></script>
  36. </body>
  37. </html>

Creating the Main Function

This code contains the main function of the application. This code will drag the object to the target drop zone, then will call the php request to remove it permanently. To make this just simple copy and write these block of codes inside the text editor, then save it as shown below. remove.php
  1. <?php
  2. if(ISSET($_REQUEST['file'])){
  3. if(unlink("files/".$_REQUEST['file'])){
  4. header('location: index.php');
  5. }
  6. }
  7. ?>
script.js Note: Make sure to save this file inside the js directory to make the script works
  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 = "remove.php?file="+data;
  10. }
  11.  
  12. function drag(e){
  13. e.dataTransfer.setData("data", e.target.id);
  14. }
There you have it we successfully created Dynamically Remove File By Dragging 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