Skip to content
PHP Logo

How can PHP arrays be utilized to exclude specific file names from being counted in a directory?

To exclude specific file names from being counted in a directory using PHP arrays, you can first retrieve all the file names in the directory using `scandir()`, then filter out the specific file names you want to exclude using `array_diff()`. This will give you an array of file names without the ones you want to exclude. You can then count the remaining files in the array to get the desired count.

php
$directory = 'path/to/directory';
$excludedFiles = ['file1.txt', 'file2.jpg']; // List of file names to exclude

$files = array_diff(scandir($directory), ['.', '..'], $excludedFiles);
$fileCount = count($files);

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