Skip to main content

PHP - Transfer File To Different Folder


In this tutorial we will create a Transfer File To Different Folder using PHP. This code will dynamically move a file when user click a button. This code use PHP POST method to call a function that can transfer file to different folder using rename() function by adding the old file and new file as a parameter 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 - Transfer File To Different Folder</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <form action="save_file.php" method="POST" enctype="multipart/form-data">
  19. <div class="form-group">
  20. <label>File:</label>
  21. <input type="file" name="file" class="form-control" required="required"/>
  22. </div>
  23. <button class="btn btn-primary" name="save">Save</button>
  24. </form>
  25. </div>
  26. <br style="clear:both;"/>
  27. <div class="col-md-8">
  28. <h4>Folder 1</h4>
  29. <div class="table-responsive">
  30. <table class="table table-bordered">
  31. <thead class="alert-info">
  32. <th>Filename</th>
  33. <th>Location</th>
  34. <th>Action</th>
  35. </thead>
  36. <tbody style="background-color:#fff;">
  37. <?php
  38. $files = scandir('folder1/');
  39. foreach($files as $file){
  40. if($file != "." && $file != ".."){
  41. ?>
  42. <tr>
  43. <td><?php echo $file?></td>
  44. <td><?php echo realpath('folder1/'.$file)?></td>
  45. <td>
  46. <form method="POST" action="transfer.php">
  47. <input type="hidden" name="file" value="<?php echo $file?>"/>
  48. <button class="btn btn-primary" name="transfer"><span class="glyphicon glyphicon-arrow-right"></span> Move</button>
  49. </form>
  50. </td>
  51. </tr>
  52. <?php
  53. }
  54. }
  55. ?>
  56. </tbody>
  57. </table>
  58. </div>
  59. </div>
  60. <div class="col-md-4">
  61. <h4>Folder 2</h4>
  62. <div class="table-responsive">
  63. <table class="table table-bordered">
  64. <thead class="alert-info">
  65. <th>Filename</th>
  66. <th>Location</th>
  67. </thead>
  68. <tbody style="background-color:#fff;"
  69. <?php
  70. $files = scandir('folder2/');
  71. foreach($files as $file){
  72. if($file != "." && $file != ".."){
  73. ?>
  74. <tr>
  75. <td><?php echo $file?></td>
  76. <td><?php echo realpath('folder2/'.$file)?></td>
  77. </tr>
  78. <?php
  79. }
  80. }
  81. ?>
  82. </tbody>
  83. </table>
  84. </div>
  85. </div>
  86. </div>
  87. </body>
  88. <script src="js/jquery-3.2.1.min.js"></script>
  89. <script src="js/bootstrap.js"></script>
  90. </html>

Creating the Save File Script

This code contains the saving file of the application.This code will store the file in a folder after submitted. 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.  
  15. if(in_array($file_ext, $ext)){
  16. $location = "folder1/".$filename;
  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 transfer a file when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as transfer.php.

  1. <?php
  2. if(ISSET($_POST['transfer'])){
  3. $file = "folder1/".$_POST['file'];
  4. $newfile = "folder2/".$_POST['file'];
  5.  
  6. if(!rename($file, $newfile)){
  7. echo "<script>alert('Failed to move ".$file."')</script>";
  8. echo "<script>window.location = 'index.php'</script>";
  9. }else{
  10. echo "<script>alert('Successfully Transfer!')</script>";
  11. echo "<script>window.location = 'index.php'</script>";
  12. }
  13. }
  14. ?>

There you have it we successfully created Transfer File To Different Folder 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

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.