How to delete selected directory

Hello,

I want to delete a directory, the example code below deletes the contents of the directory but does not delete the “bulut” directory. How can I delete the “bulut” directory?

$dir = 'bulut';
delete_directory($dir);
function delete_directory($dirPath){
        $dir = $dirPath;   
        if(is_dir($dir)){
            $files = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST
            );
            foreach($files as $file){
                if ($file->isDir()){
                    rmdir($file->getRealPath());
                }else{
                    unlink($file->getRealPath());
                }
            }
            rmdir($dir);
        }
}

When I added an if check to the rmdir() function, the “bulut” directory was deleted.

$dir = 'bulut';
delete_directory($dir);
function delete_directory($dirPath){
        $dir = $dirPath;   
        if(is_dir($dir)){
            $files = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST
            );
            foreach($files as $file){
                if ($file->isDir()){
                    rmdir($file->getRealPath());
                }else{
                    unlink($file->getRealPath());
                }
            }
            if(rmdir($dir)) 
            { 
            echo ($dir . " successfully removed"); 
            } 
            else
            { 
            echo ($dir . " couldn't be removed");  
            }
        }
}
Sponsor our Newsletter | Privacy Policy | Terms of Service