PHP - Simple Copy File By Dragging

Language

In this tutorial we will create a Simple Copy File By Dragging using PHP. This code will allow the user to copy a file by dragging it into the drop zone. By the use of JavaScript it is clearly that it made the application much easier than by doing it manually. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language that is used primarily on any webserver including xamp, wamp, etc. 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 Copy 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. <th>Action</th>
  27. </tr>
  28. </thead>
  29. <tbody>
  30. <?php
  31. $files = scandir('file1/');
  32. $count=0;
  33. foreach ($files as $file){
  34. if($file != '.' && $file !='..'){
  35. ?>
  36. <tr>
  37. <td>
  38. <?php
  39. if(file_exists("file2/".$file)){
  40. ?>
  41. <label><?php echo $file?></label>
  42. <?php
  43. }else{
  44. ?>
  45. <label draggable="true" id="<?php echo $file?>" ondragstart="drag(event);"><?php echo $file?></label>
  46. <?php
  47. }
  48. ?>
  49. </td>
  50. <td>
  51. <?php
  52. if(file_exists("file2/".$file)){
  53. ?>
  54. <center>Copied</center>
  55. <?php
  56. }
  57. ?>
  58. </td>
  59. </tr>
  60. <?php
  61. }
  62. }
  63. ?>
  64.  
  65. </tbody>
  66. </table>
  67. </div>
  68. </div>
  69. <div class="col-md-2" style="border:5px solid #ccc; padding:10px;">
  70. <img src="images/copy.png" width="100%" ondrop="drop(event)" ondragover="dragOver(event)"/>
  71. </div>
  72. <div class="col-md-5">
  73. <h4>File 2</h3>
  74. <div class="table-responsive">
  75. <table class="table table-bordered">
  76. <thead class="alert-info">
  77. <tr>
  78. <th>Filename</th>
  79. </tr>
  80. </thead>
  81. <tbody>
  82. <?php
  83. $files = scandir('file2/');
  84. foreach ($files as $file){
  85. if($file != '.' && $file !='..'){
  86. ?>
  87. <tr>
  88. <td><?php echo $file?></td>
  89. </tr>
  90. <?php
  91. }
  92. }
  93. ?>
  94. </tbody>
  95. </table>
  96. </div>
  97. </div>
  98. </div>
  99. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  100. <div class="modal-dialog" role="document">
  101. <form action="save_file.php" method="POST" enctype="multipart/form-data">
  102. <div class="modal-content">
  103. <div class="modal-body">
  104. <div class="col-md-3"></div>
  105. <div class="col-md-6">
  106. <form method="POST" action="">
  107. <div class="form-group">
  108. <label>File:</label>
  109. <input type="file" name="file" class="form-control" required="required"/>
  110. </div>
  111. </form>
  112. </div>
  113. </div>
  114. <div style="clear:both;"></div>
  115. <div class="modal-footer">
  116. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  117. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  118. </div>
  119. </div>
  120. </form>
  121. </div>
  122. </div>
  123. </body>
  124. <script src="js/jquery-3.2.1.min.js"></script>
  125. <script src="js/bootstrap.js"></script>
  126. <script src="js/script.js"></script>
  127. </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 copy 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. copy_file.php
  1. <?php
  2. if(ISSET($_REQUEST['file'])){
  3. $file = "file1/".$_REQUEST['file'];
  4. $newfile = "file2/".$_REQUEST['file'];
  5.  
  6. if(!copy($file, $newfile)){
  7. echo "<script>alert('Failed to copy ".$file."')</script>";
  8. echo "<script>window.location = 'index.php'</script>";
  9. }else{
  10. echo "<script>alert('Copied!')</script>";
  11. echo "<script>window.location = 'index.php'</script>";
  12. }
  13. }
  14. ?>
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 = "copy_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 Copy 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment