php remove directory not empty

Solutions on MaxInterview for php remove directory not empty by the best coders in the world

showing results for - "php remove directory not empty"
Alexy
01 Jan 2017
1function deleteDirectory($dir) {
2    if (!file_exists($dir)) {
3        return true;
4    }
5
6    if (!is_dir($dir)) {
7        return unlink($dir);
8    }
9
10    foreach (scandir($dir) as $item) {
11        if ($item == '.' || $item == '..') {
12            continue;
13        }
14
15        if (!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
16            return false;
17        }
18
19    }
20
21    return rmdir($dir);
22}
Amelie
12 Jan 2019
1<?php
2
3    function removeEmptyDirs($path, $checkUpdated = false, $report = false) {
4        $dirs = glob($path . "/*", GLOB_ONLYDIR);
5
6        foreach($dirs as $dir) {
7            $files = glob($dir . "/*");
8            $innerDirs = glob($dir . "/*", GLOB_ONLYDIR);
9            if(empty($files)) {
10                if(!rmdir($dir))
11                    echo "Err: " . $dir . "<br />";
12               elseif($report)
13                    echo $dir . " - removed!" . "<br />";
14            } elseif(!empty($innerDirs)) {
15                removeEmptyDirs($dir, $checkUpdated, $report);
16                if($checkUpdated)
17                    removeEmptyDirs($path, $checkUpdated, $report);
18            }
19        }
20
21    }
22
23
24?>
25