Tutorial: Dynamically Remove Folder in PHP
In this tutorial, we will create a Dynamically Remove Folder using PHP. This code will dynamically remove a folder when the user clicks the button in the table row. This code uses a simple href or hypertext reference to call a script within the server that sends the folder name, which removes the file using remover() 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 manages a database server to handle a bulk of data per transaction. It describes as an advanced technology that manages both server and control-block of your machine.
Getting Started:
First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the 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 a copy of the structure, and write it into your text editor, 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-3"></div>
<div class="col-md-6 well">
<h3 class="text-primary">PHP - Dynamically Remove Folder</h3>
<hr style="border-top:1px dotted #ccc;"/>
<div class="col-md-6">
<table class="table table-bordered">
<thead class="alert-info">
<tr>
<th>Folder Name</th>
<th>Action</th>
</tr>
</thead>
<tbody style="background-color:#fff;">
<?php
$files = scandir('files/');
foreach ($files as $file){
if($file != '.' && $file !='..'){
?>
<tr>
<td><?php echo $file?></td>
<td><a class="btn btn-danger" href="remove.php?folder=<?php echo $file?>"><span class="glyphicon glyphicon-trash"></span> Remove</a></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
</div>
</body>
</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.
<?php
function removeDir($dir) {
$items = scandir($dir);
foreach ($items as $item) {
if ($item === '.' || $item === '..') {
continue;
}
$path = $dir.'/'.$item;
if (is_dir($path)) {
xrmdir($path);
} else {
unlink($path);
}
}
rmdir($dir);
}
if(ISSET($_REQUEST['folder'])){
$folder = $_REQUEST['folder'];
if(file_exists("files/".$folder)){
removeDir("files/".$folder);
header("location: index.php");
}
}
?>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!















