What potential pitfalls should be considered when using is_dir in PHP for directory listing?

When using is_dir in PHP for directory listing, one potential pitfall to consider is that is_dir only checks if a given path is a directory, but it does not guarantee that the directory actually exists. To avoid errors when listing directories, it is recommended to first check if the directory exists using is_dir and then proceed with listing its contents.

$directory = "path/to/directory";

if(is_dir($directory)) {
    $files = scandir($directory);
    
    foreach($files as $file) {
        if($file != '.' && $file != '..') {
            echo $file . "<br>";
        }
    }
} else {
    echo "Directory does not exist.";
}