What is the function of opendir in PHP and how can it be used to read subdirectories?

The opendir function in PHP is used to open a directory handle, which can then be used to read the contents of the directory. To read subdirectories, you can use a combination of opendir, readdir, and is_dir functions to iterate through the contents of the directory and identify subdirectories.

$dir = "path/to/directory";

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if (is_dir($dir . '/' . $file) && $file != '.' && $file != '..') {
                echo "Subdirectory: " . $file . "<br>";
            }
        }
        closedir($dh);
    }
}