Is using the filetype function to check if a file is a directory before processing it a recommended best practice in PHP file handling?

When processing files in PHP, it is recommended to use the `is_dir()` function to check if a file is a directory before attempting to process it. This helps avoid potential errors or unexpected behavior when working with files in a directory.

$filename = 'example_directory';

if (is_dir($filename)) {
    // Process the directory
    $files = scandir($filename);
    foreach ($files as $file) {
        // Process each file in the directory
    }
} else {
    // Handle the case where $filename is not a directory
    echo 'The specified file is not a directory.';
}