PHP - Transfer File To Different Folder

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

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


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.