Iterate files and directories in PHP – Fast tips

Valerio Barbera
iterate files and directories in PHP

I recently had the need to deal with some tasks that require knowledge of raw PHP functions like iterate files and directories. As we develop ever higher levels of abstraction, we often no longer remember how to work at a low level. In reality, there are many implications, especially in terms of performance.

As I carry on these optimizations I’m noticing that my application supports increasing workloads, despite the decrease in the consumption of hardware resources. I Hope the snippets below can help not only young developers but also seasoned professionals who are looking for performance optimizations ideas.

There are several options to iterate files and directories in PHP:

  • glob() function
  • RecursiveDirectoryIterator class
  • scandir() function
  • PHP generators

Method 1: Using “glob()”

The glob function extract files and directories that match a pattern. It is useful when you need to specify a sort of rule to extract items from the filesystem structure.

You can also pass a second parameters as a costant to get only directories.

<?php
$directory = 'path/to/directory'; // Replace with the actual directory path

// Extracts files and directories that match a pattern
$items = glob($directory . '/*');
foreach ($items as $item) {
    if (is_file($item)) {
        echo "File: {$item}\n";
    }

    if (is_dir($item)) {
        echo "Directory: {$item}\n";
    }
}

// Iterate over directories
$directories = glob($directory . '/*', GLOB_ONLYDIR);
foreach ($directories as $dir) {
    echo "Directory: $dir\n";
}

Method 2: Using “RecursiveDirectoryIterator” class

This is obviously a more Object Oriented approach.

<?php
$directory = 'path/to/directory'; // Replace with the actual directory path

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS),
    RecursiveIteratorIterator::SELF_FIRST
);

foreach ($iterator as $item) {
    $path = $item->getPathname();
    if ($item->isFile()) {
        echo "File: {$path}\n";
    } elseif ($item->isDir()) {
        echo "Directory: {$path}\n";
    }
}

Method 3: Using “scandir()”

The scandir() function is another option available in PHP to iterate over files and directories. It returns an array of file and directory names including current directory (“.”) and parent directory (“..”).

<?php
$directory = 'path/to/directory'; // Replace with the actual directory path

$entries = scandir($directory);
foreach ($entries as $entry) {
    if ($entry !== '.' && $entry !== '..') {
        $path = $directory . '/' . $entry;
        if (is_file($path)) {
            echo "File: {$path}\n";
        } elseif (is_dir($path)) {
            echo "Directory: {$path}\n";
        }
    }
}

In this example The foreach loop iterates over each entry, excluding the current directory (‘.’) and parent directory (‘..’). The $path variable is constructed by concatenating the directory path and the entry name.

Method 4: Using “generators”

PHP generators can be used to iterate over files and directories. Generators provide a memory-efficient way to generate a sequence of values on-the-fly, which is useful when dealing with large sets of files or directories.

Here’s an example that demonstrates how to use a generator to iterate over files in a directory:

<?php

// Function that returns yields
function iterateFiles($directory) {
    $files = scandir($directory);
    foreach ($files as $file) {
        if ($file !== '.' && $file !== '..') {
            $path = $directory . '/' . $file;
            if (is_file($path)) {
                yield $path;
            }
        }
    }
}

$directory = 'path/to/directory'; // Replace with the actual directory path

$fileGenerator = iterateFiles($directory);
foreach ($fileGenerator as $file) {
    echo "File: $file\n";
}

Remeber, any function that return yields is a generator. As explained in the official documentation:

When a generator function is called, it returns an object that can be iterated over. When you iterate over that object, PHP will call the object’s iteration methods each time it needs a value, then saves the state of the generator when the generator yields a value so that it can be resumed when the next value is required.

Save and resume the state of the generator allows for a super efficient use of the system memory.

You can follow me on Linkedin or X. I post about building my SaaS business.

Monitor your application for free

Inspector is a Code Execution Monitoring tool specifically designed for software developers. You don’t need to install anything in the infrastructure, just install the composer package and you are ready to go.

Unlike other complex, all-in-one platforms, Inspector is super easy, and PHP friendly. You can try our Laravel or Symfony package.

If you are looking for HTTP monitoring, query insights, and the ability to forward alerts and notifications into your messaging environment try Inspector for free. Register your account.

Or learn more on the website: https://inspector.dev

Related Posts

Managing Human-in-the-Loop With Checkpoints – Neuron Workflow

The integration of human oversight into AI workflows has traditionally been a Python-dominated territory, leaving PHP developers to either compromise on their preferred stack or abandon sophisticated agentic patterns altogether. The new checkpointing feature in Neuron’s Workflow component continues to strengthen the dynamic of bringing production-ready human-in-the-loop capabilities directly to PHP environments. Checkpointing addresses a

Monitor Your PHP Applications Through Your AI Assistant – Inspector MCP server

You push code, hope it works, and discover issues when users complain or error rates spike. Traditional monitoring tools require constant context switching—jumping between your IDE, terminal, dashboard tabs, and documentation. This friction kills productivity and delays problem resolution. Inspector’s new MCP server changes this dynamic by connecting your AI coding assistant directly to your

High-Perfomance Tokenizer in PHP

When I launched Neuron AI Framework six months ago, I wasn’t certain PHP could compete with Python’s AI ecosystem. The framework’s growth to over 1,000 GitHub stars in five months changed that perspective entirely. What began as an experiment in bringing modern AI agents capabilities to PHP has evolved into something more substantial: proof that