• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

PHP - Transfer File To Different Folder

kikeelrey

Content Licensing Manager
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
101
Likes
28
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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. if

    (

    $filesize

    >

    500000

    )

    {
  7. echo

    "<script>alert('File too large to upload')</script>"

    ;
  8. echo

    "<script>window.location = 'index.php'</script>"

    ;
  9. }

    else

    {
  10. $file

    =

    explode

    (

    "."

    ,

    $filename

    )

    ;
  11. $file_ext

    =

    end

    (

    $file

    )

    ;
  12. $ext

    =

    array

    (

    "png"

    ,

    "jpg"

    ,

    "jpeg"

    )

    ;

  13. if

    (

    in_array

    (

    $file_ext

    ,

    $ext

    )

    )

    {
  14. $location

    =

    "folder1/"

    .

    $filename

    ;
  15. if

    (

    move_uploaded_file

    (

    $filetemp

    ,

    $location

    )

    )

    {
  16. echo

    "<script>alert('File Saved!')</script>"

    ;
  17. echo

    "<script>window.location = 'index.php'</script>"

    ;
  18. }
  19. }

    else

    {
  20. echo

    "<script>alert('Only images allowed')</script>"

    ;
  21. echo

    "<script>window.location = 'index.php'</script>"

    ;
  22. }
  23. }
  24. }
  25. ?>

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. if

    (

    !

    rename

    (

    $file

    ,

    $newfile

    )

    )

    {
  6. echo

    "<script>alert('Failed to move "

    .

    $file

    .

    "')</script>"

    ;
  7. echo

    "<script>window.location = 'index.php'</script>"

    ;
  8. }

    else

    {
  9. echo

    "<script>alert('Successfully Transfer!')</script>"

    ;
  10. echo

    "<script>window.location = 'index.php'</script>"

    ;
  11. }
  12. }
  13. ?>

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!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,512

356,407

356,429

Top
Raidforums