What are some best practices for excluding specific file extensions when counting files in PHP?

When counting files in a directory using PHP, it may be necessary to exclude specific file extensions from the count. One way to achieve this is by using the glob function to retrieve a list of files in the directory, filtering out the files with the undesired extensions, and then counting the remaining files.

$directory = '/path/to/directory/';
$excludedExtensions = array('txt', 'pdf');

$files = glob($directory . '*');
$filteredFiles = array_filter($files, function($file) use ($excludedExtensions) {
    $extension = pathinfo($file, PATHINFO_EXTENSION);
    return !in_array($extension, $excludedExtensions);
});

$fileCount = count($filteredFiles);

echo "Number of files in directory: " . $fileCount;