PHP - Simple Copy File By Dragging

Submitted by: 
Language: 
Visitors have accessed this post 221 times.

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

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