PHP - Create Directory Dynamically

In this tutorial we will create a Create Directory Dynamically Using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the 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. 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>
  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://www.sourcecodester.com/">Sourcecodester.com</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Create Directory Dynamically</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <form method="POST" action="create_dir.php" class="form-inline">
  18.                         <label>Enter a text</label>
  19.                         <input type="text" name="dir" class="form-control">
  20.                         <button class="btn btn-primary" name="add"><span class="glyphicon glyphicon-plus"></span></button>
  21.                 </form>
  22.                 <br /><br />
  23.                 <table class="table table-bordered">
  24.                         <thead class="alert-success">
  25.                                 <tr>
  26.                                         <th>Directory</th>
  27.                                         <th>Location</th>
  28.                                 </tr>
  29.                         </thead>
  30.                         <tbody style="background-color:#fff;">
  31.                                 <?php
  32.                                         $files = scandir('.');
  33.                                         foreach($files as $file){
  34.                                                 if($file != "." && $file != ".." && $file != "css" && $file != "fonts" && is_dir($file)){
  35.                                 ?>
  36.                                         <tr>
  37.                                                 <td><?php echo $file."<br />";?></td>
  38.                                                 <td><?php echo realpath($file) ?></td>
  39.                                         </tr>
  40.                                 <?php
  41.                                                 }
  42.                                                
  43.                                         }
  44.                                 ?>
  45.                                
  46.                         </tbody>
  47.                 </table>
  48.         </div>
  49. </body>
  50. </html>

Creating the Main Function

This code contains the main function of the application. This code will create a directory on a target path. To make this just simple copy and write the this block of codes inside the text editor, then save it as create_dir.php.
  1. <?php
  2.         if(ISSET($_POST['add'])){
  3.                 $dir = str_replace(" ", "", $_POST['dir']);
  4.                 if(!file_exists($dir)){
  5.                         mkdir($dir);
  6.                         echo "<script>alert('You create directory successfully')</script>";
  7.                         echo "<script>window.location='index.php'</script>";
  8.                 }
  9.         }
  10. ?>
There you have it we successfully created Create Directory Dynamically 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