How To Delete Folder using PHP

0 1,920

Hi Guys, in this article, we will show you How To Delete Folder using PHP. this article we use rmdir()  Function to delete local directory or server side easily. To delete all files and directories in all sub-directories, we can make use of recursion.

Getting Bootstrap

Bootstrap can be Download to your needs from their Getting Started page but I would prefer using the CDN option, because it is faster also it is advised to go through and get yourself accustomed with some bootstrap terms, including common classes. This page also some Examples of how to use Bootstrap classes.

How To Delete Folder using PHP

1 – Creating Index Page

Here we are going to create an interface to get all folders and creating a remove button to delete easily. here the source code you can save it as index.php in your project folder.

<!DOCTYPE html>
<html lang="en">
    <title>How To Delete Folder using PHP</title>
    <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
    <style type="text/css">
      body {
        margin-top: 150px;

      .well {
        -webkit-box-shadow: 1px 2px 11px 1px rgba(0, 0, 0, 0.75);
        -moz-box-shadow: 1px 2px 11px 1px rgba(0, 0, 0, 0.75);
        box-shadow: 1px 2px 11px 1px rgba(0, 0, 0, 0.75);
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
      <h3 class="text-success">How TO Delete Folder using PHP</h3>
      <hr style="border-top:1px dotted #ccc;" />
      <table class="table table-bordered">
        <thead class="alert-success">
            <th>Folder Name</th>
        <tbody style="background-color:#fff;"> <?php $files = scandir('files/'); foreach ($files as $file){ if($file != '.' && $file !='..'){ ?> <tr>
              <img src="img/folder.png"> <?php echo $file?>
              <a class="btn btn-danger" href="delete.php?directory=
                                    <?php echo $file?>">Remove </a>
          </tr> <?php } } ?> </tbody>

2 – Delete Folder using PHP

here we are creating the main function of this project. and this step, we use rmdir()  Function to delete local or server-side directory and its content. here the source code you can save it as delete.php in your project folder.

<?php if (ISSET($_REQUEST['directory'])) {
    $directory = $_REQUEST['directory'];
    if (file_exists("files/" . $directory)) {
        removeDir("files/" . $directory);
        header("location: index.php");
function removeDir($dir) {
    $items = scandir($dir);
    foreach ($items as $item) {
        if ($item === '.' || $item === '..') {
        $path = $dir . '/' . $item;
        if (is_dir($path)) {
        } else {
} ?>

If you facing any type of problem with this source code then you can Download the Complete source code in zip Formate by clicking the below button Download Now otherwise you can send Comment.

Download Source Code

Leave A Reply

Your email address will not be published.