PHP - Move File To Other Directory
Submitted by razormist on Wednesday, August 29, 2018 - 15:04.
In this tutorial we will create a Move File To Other Directory using PHP. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML. It is designed to make it easier to navigate a document, via classes and id's attribute. So let's now do the coding...
There you have it we successfully created Move File To Other Directory 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!
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. Lastly, 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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-2"></div>
- <div class="col-md-8 well">
- <h3 class="text-primary">PHP - Move File To Other Directory</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <button class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add File</button>
- <br /><br />
- <div class="col-md-6">
- <h4>Directory 1</h4>
- <div class="table-responsive">
- <table class="table table-bordered">
- <thead class="alert-info">
- <th>Filename</th>
- <th>Location</th>
- <th>Action</th>
- </thead>
- <tbody style="background-color:#fff;">
- <?php
- foreach($files as $file){
- if($file != "." && $file != ".."){
- ?>
- <tr>
- <td><?php echo $file?></td>
- <td>
- <form method="POST" action="move_to.php">
- <input type="hidden" name="file" value="<?php echo $file?>"/>
- <button class="btn btn-primary" name="move_to"><span class="glyphicon glyphicon-arrow-right"></span> Move</button>
- </form>
- </td>
- </tr>
- <?php
- }
- }
- ?>
- </tbody>
- </table>
- </div>
- </div>
- <div class="col-md-6">
- <h4>Directory 2</h4>
- <div class="table-responsive">
- <table class="table table-bordered">
- <thead class="alert-info">
- <th>Filename</th>
- <th>Location</th>
- <th>Action</th>
- </thead>
- <tbody style="background-color:#fff;"
- <?php
- foreach($files as $file){
- if($file != "." && $file != ".."){
- ?>
- <tr>
- <td><?php echo $file?></td>
- <td>
- <form method="POST" action="move_to.php">
- <input type="hidden" name="file" value="<?php echo $file?>"/>
- <button class="btn btn-primary" name="move_back"><span class="glyphicon glyphicon-arrow-left"></span> Move</button>
- </form>
- </td>
- </tr>
- <?php
- }
- }
- ?>
- </tbody>
- </table>
- </div>
- </div>
- </div>
- <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
- <div class="modal-dialog" role="document">
- <form action="save_file.php" method="POST" enctype="multipart/form-data">
- <div class="modal-content">
- <div class="modal-body">
- <div class="col-md-3"></div>
- <div class="col-md-6">
- <form method="POST" action="">
- <div class="form-group">
- <label>File:</label>
- <input type="file" name="file" class="form-control" required="required"/>
- </div>
- </form>
- </div>
- </div>
- <div style="clear:both;"></div>
- <div class="modal-footer">
- <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
- <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
- </div>
- </div>
- </form>
- </div>
- </div>
- </body>
- <script src="js/jquery-3.2.1.min.js"></script>
- <script src="js/bootstrap.js"></script>
- </html>
Creating Save File Function
This code contains the save function of the application. This code will get and store the file to the corresponding directory. To do that just copy and write this block of codes inside the text editor, then save it as save_file.php.- <?php
- $filename = $_FILES['file']['name'];
- $filesize = $_FILES['file']['size'];
- $filetemp = $_FILES['file']['tmp_name'];
- if($filesize > 500000){
- echo "<script>alert('File too large to upload')</script>";
- echo "<script>window.location = 'index.php'</script>";
- }else{
- $location = "directory1/".$filename;
- echo "<script>alert('File Saved!')</script>";
- echo "<script>window.location = 'index.php'</script>";
- }
- }else{
- echo "<script>alert('Only images allowed')</script>";
- echo "<script>window.location = 'index.php'</script>";
- }
- }
- }
- ?>
Creating Main Function
This code contains the main function of the application. This code will move the file from the directory to other directory when the button is clicked. To do that just copy and write this block of codes inside the text editor, then save it as move_to.php.- <?php
- $file = "directory1/".$_POST['file'];
- $newfile = "directory2/".$_POST['file'];
- echo "<script>alert('Failed to move ".$file."')</script>";
- echo "<script>window.location = 'index.php'</script>";
- }else{
- echo "<script>alert('Moved!')</script>";
- echo "<script>window.location = 'index.php'</script>";
- }
- }
- $file = "directory2/".$_POST['file'];
- $newfile = "directory1/".$_POST['file'];
- echo "<script>alert('Failed to move ".$file."')</script>";
- echo "<script>window.location = 'index.php'</script>";
- }else{
- echo "<script>alert('Moved!')</script>";
- echo "<script>window.location = 'index.php'</script>";
- }
- }
- ?>
Add new comment
- 6 views