Dynamically Remove Folder In PHP | CampCodes

Dynamically Remove Folder in PHP

August 27, 2020
dynamically remove folder in php

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">
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
    <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">
                        <th>Folder Name</th>
                <tbody style="background-color:#fff;">
                        $files = scandir('files/');
                        foreach ($files as $file){
                            if($file != '.' && $file !='..'){
                        <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>

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.

    function removeDir($dir) {
        $items = scandir($dir);
        foreach ($items as $item) {
            if ($item === '.' || $item === '..') {
            $path = $dir.'/'.$item;
            if (is_dir($path)) {
            } else {
        $folder = $_REQUEST['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!

Leave a Reply

Your email address will not be published. Required fields are marked *