PHP - Dynamically Remove Folder

In this tutorial we will create a Dynamically Remove Folder using PHP. This code will dynamically remove a folder when user click the button in the table row. This code use a simple href or hypertext reference to call a script within the server that send the folder name which remove the file using removeDir() and by adding the folder path as a parameter.This is a user-friendly kind of program feel free to modify 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.        
  8. <body>
  9.         <nav class="navbar navbar-default">
  10.                 <div class="container-fluid">
  11.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12.                 </div>
  13.         </nav>
  14.         <div class="col-md-3"></div>
  15.         <div class="col-md-6 well">
  16.                 <h3 class="text-primary">PHP - Dynamically Remove Folder</h3>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-6">
  19.                         <table class="table table-bordered">
  20.                                 <thead class="alert-info">
  21.                                         <tr>
  22.                                                 <th>Folder Name</th>
  23.                                                 <th>Action</th>
  24.                                         </tr>
  25.                                 </thead>
  26.                                 <tbody style="background-color:#fff;">
  27.                                         <?php
  28.                                                 $files = scandir('files/');
  29.                                                 foreach ($files as $file){
  30.                                                         if($file != '.' && $file !='..'){
  31.                                         ?>
  32.                                         <tr>
  33.                                                 <td><?php echo $file?></td>
  34.                                                 <td><a class="btn btn-danger" href="remove.php?folder=<?php echo $file?>"><span class="glyphicon glyphicon-trash"></span> Remove</a></td>
  35.                                         </tr>
  36.                                         <?php
  37.                                                         }
  38.                                                 }
  39.                                         ?>
  40.                                 </tbody>
  41.                         </table>
  42.                 </div>
  43.         </div>
  44. </body>
  45. </html>

Creating the Main Function

This code contains the main function of the application. This code will remove a file when the button is clicked. To do that just copy and write this block of codes inside the text editor, then save it as remove.php.
  1. <?php
  2.         function removeDir($dir) {
  3.                 $items = scandir($dir);
  4.                 foreach ($items as $item) {
  5.                         if ($item === '.' || $item === '..') {
  6.                                 continue;
  7.                         }
  8.                         $path = $dir.'/'.$item;
  9.                         if (is_dir($path)) {
  10.                                 xrmdir($path);
  11.                         } else {
  12.                                 unlink($path);
  13.                         }
  14.                 }
  15.                 rmdir($dir);
  16.         }
  17.        
  18.         if(ISSET($_REQUEST['folder'])){
  19.                 $folder = $_REQUEST['folder'];
  20.                 if(file_exists("files/".$folder)){
  21.                         removeDir("files/".$folder);
  22.                         header("location: index.php");
  23.                 }
  24.         }
  25.        
  26. ?>
There you have it we successfully created Dynamically Remove 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