PHP - Simple Cut File By Dragging

In this tutorial we will create a Simple Cut File By Dragging using PHP. This code will dynamically allow the user to move a file by dragging it to the target zone. It use a JavaScript Drag and Drop event to send the id of the object, and send to php server to move the file to other directory. A user-friendly program that can be modified, feel free to work around with it. We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine.

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="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-2"></div>
  14. <div class="col-md-8 well">
  15. <h3 class="text-primary">PHP - Simple Cut File By Dragging</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add file</button>
  18. <br /><br />
  19. <div class="col-md-5">
  20. <h4>File 1</h3>
  21. <div class="table-responsive">
  22. <table class="table table-bordered">
  23. <thead class="alert-info">
  24. <tr>
  25. <th>Filename</th>
  26. </tr>
  27. </thead>
  28. <tbody>
  29. <?php
  30. $files = scandir('file1/');
  31. $count=0;
  32. foreach ($files as $file){
  33. if($file != '.' && $file !='..'){
  34. ?>
  35. <tr>
  36. <td><label draggable="true" id="<?php echo $file?>-move1" ondragstart="drag(event);"><?php echo $file?></label></td>
  37. </tr>
  38. <?php
  39. }
  40. }
  41. ?>
  42.  
  43. </tbody>
  44. </table>
  45. </div>
  46. </div>
  47. <div class="col-md-2" style="border:5px solid #ccc; padding:10px;">
  48. <img src="images/cut.png" width="100%" ondrop="drop(event)" ondragover="dragOver(event)"/>
  49. </div>
  50. <div class="col-md-5">
  51. <h4>File 2</h3>
  52. <div class="table-responsive">
  53. <table class="table table-bordered">
  54. <thead class="alert-info">
  55. <tr>
  56. <th>Filename</th>
  57. </tr>
  58. </thead>
  59. <tbody>
  60. <?php
  61. $files = scandir('file2/');
  62. foreach ($files as $file){
  63. if($file != '.' && $file !='..'){
  64. ?>
  65. <tr>
  66. <td><label draggable="true" id="<?php echo $file?>-move2" ondragstart="drag(event);"><?php echo $file?></label></td>
  67. </tr>
  68. <?php
  69. }
  70. }
  71. ?>
  72. </tbody>
  73. </table>
  74. </div>
  75. </div>
  76. </div>
  77. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  78. <div class="modal-dialog" role="document">
  79. <form action="save_file.php" method="POST" enctype="multipart/form-data">
  80. <div class="modal-content">
  81. <div class="modal-body">
  82. <div class="col-md-3"></div>
  83. <div class="col-md-6">
  84. <form method="POST" action="">
  85. <div class="form-group">
  86. <label>File:</label>
  87. <input type="file" name="file" class="form-control" required="required"/>
  88. </div>
  89. </form>
  90. </div>
  91. </div>
  92. <div style="clear:both;"></div>
  93. <div class="modal-footer">
  94. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  95. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  96. </div>
  97. </div>
  98. </form>
  99. </div>
  100. </div>
  101. </body>
  102. <script src="js/jquery-3.2.1.min.js"></script>
  103. <script src="js/bootstrap.js"></script>
  104. <script src="js/script.js"></script>
  105. </html>

Creating the Save File Script

This code contains the saving file of the application.This code will store the file in the directory after the user click the submit button. To do that just copy and write this block of codes inside the text editor, then save it as save_file.php.
  1. <?php
  2. if(ISSET($_POST['save'])){
  3. $filename = $_FILES['file']['name'];
  4. $filesize = $_FILES['file']['size'];
  5. $filetemp = $_FILES['file']['tmp_name'];
  6.  
  7. if($filesize > 500000){
  8. echo "<script>alert('File too large to upload')</script>";
  9. echo "<script>window.location = 'index.php'</script>";
  10. }else{
  11. $file = explode(".", $filename);
  12. $file_ext = end($file);
  13. $ext = array("png", "jpg", "jpeg");
  14. $newFile = time().".".$file_ext;
  15. if(in_array($file_ext, $ext)){
  16. $location = "file1/".$newFile;
  17. if(move_uploaded_file($filetemp, $location)){
  18. echo "<script>alert('File Saved!')</script>";
  19. echo "<script>window.location = 'index.php'</script>";
  20. }
  21. }else{
  22. echo "<script>alert('Only images allowed')</script>";
  23. echo "<script>window.location = 'index.php'</script>";
  24. }
  25. }
  26. }
  27. ?>

Creating the Main Function

This code contains the main function of the application. This code will move the file when it is drag and drop in the target zone. To make this just copy and write these block of codes below inside the text editor, then save it as shown below. move_file.php
  1. <?php
  2. if(ISSET($_REQUEST['file'])){
  3. $files = $_REQUEST['file'];
  4. $exp = explode("-", $files);
  5. echo $exp[0];
  6. if($exp[1]== "move1"){
  7. $files = "file1/".$exp[0];
  8. $newfile = "file2/".$exp[0];
  9.  
  10. if(!rename($files, $newfile)){
  11. echo "<script>alert('Failed to move ".$exp[0]."')</script>";
  12. echo "<script>window.location = 'index.php'</script>";
  13. }else{
  14. echo "<script>alert('File have been move!')</script>";
  15. echo "<script>window.location = 'index.php'</script>";
  16. }
  17. }
  18.  
  19. if($exp[1]== "move2"){
  20. $files = "file2/".$exp[0];
  21. $newfile = "file1/".$exp[0];
  22.  
  23. if(!rename($files, $newfile)){
  24. echo "<script>alert('Failed to move ".$exp[0]."')</script>";
  25. echo "<script>window.location = 'index.php'</script>";
  26. }else{
  27. echo "<script>alert('File have been move!')</script>";
  28. echo "<script>window.location = 'index.php'</script>";
  29. }
  30. }
  31. }
  32. ?>
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 = "move_file.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 Simple Cut 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