What potential issues can arise when counting files in a folder using PHP?

One potential issue that can arise when counting files in a folder using PHP is that the function may not be able to access the directory due to permission restrictions. To solve this issue, you can check if the directory is readable before attempting to count the files.

$directory = 'path/to/directory';

if (is_readable($directory)) {
    $files = scandir($directory);
    $fileCount = count($files) - 2; // Subtracting 2 to account for '.' and '..' entries
    echo "Number of files in directory: " . $fileCount;
} else {
    echo "Unable to read directory. Check file permissions.";
}