Are there any best practices for using glob() function in PHP to check for file presence in directories?

When using the glob() function in PHP to check for file presence in directories, it is important to handle the returned array properly. It is best practice to check if the glob() function returns false (indicating an error) before using the array of files. Additionally, you should use the GLOB_BRACE flag to search for multiple file types at once.

// Check for file presence in directories using glob() function
$files = glob('/path/to/directory/*.txt', GLOB_BRACE);

if ($files !== false) {
    foreach ($files as $file) {
        echo "Found file: $file\n";
    }
} else {
    echo "Error while searching for files\n";
}