What is the recommended function for deleting directories in PHP, apart from "unlink"?

When deleting directories in PHP, the recommended function to use is "rmdir" instead of "unlink". The "rmdir" function is specifically designed to remove directories, while "unlink" is used for deleting files. Using "rmdir" ensures that the directory and all its contents are properly removed.

// Delete a directory and its contents using rmdir
function deleteDirectory($dir) {
    if (!file_exists($dir)) {
        return false;
    }
    
    $files = array_diff(scandir($dir), array('.','..'));
    foreach ($files as $file) {
        (is_dir("$dir/$file")) ? deleteDirectory("$dir/$file") : unlink("$dir/$file");
    }
    
    return rmdir($dir);
}

// Example usage
deleteDirectory('/path/to/directory');