Skip to content

How to Remove Directories Recursively with PHP ?

You can’t delete non-empty directories with one of PHPs own functions so in order to delete Directories Recursively just fallow the below shown logic,shown PHP function scans a given directory and deletes all files and sub directories it finds.

Here is the function

    function rmdir_recursive($dir){
    $files = scandir($dir);
    foreach ($files as $file) {
    if ($file == '.' OR $file == '..') continue;
    $file = "$dir/$file";
    if (is_dir($file)) {
    rmdir_recursive($file);
    rmdir($file);
    } else {
    unlink($file);
    }
    }
    rmdir($dir);
    }
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments