How can subdirectories be excluded when reading files in PHP?

When reading files in PHP, you can exclude subdirectories by checking if the current item is a file before processing it. This can be achieved by using the is_file() function to filter out directories from the list of files returned by functions like scandir() or glob().

$directory = '/path/to/directory';

$files = scandir($directory);

foreach ($files as $file) {
    if (is_file($directory . '/' . $file)) {
        // Process the file here
        echo $file . "\n";
    }
}