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

How to Stop a Streamed AI Response Mid-Flight in Neuron AI v3

One thing I didn’t anticipate when building Neuron AI was how many edge cases would surface not from the AI integration itself, but from the UI layer sitting on top of it. Developers don’t just want agents that work. They want agents that feel right to use. And the moment you start building chat interfaces

Conversational Data Collection: Introducing AIForm

One of the more interesting things about building an open-source framework is that the community often knows what to build next before you do. When I started Neuron AI, I had a fairly clear picture in my head of the core primitives: agents, tools, workflows, structured output. What I didn’t fully anticipate was how quickly

Neuron AI Now Supports ZAI — The GLM Series Is Worth Your Attention

There’s a pattern I’ve noticed over the past year while working on Neuron AI: the decisions that matter most are rarely about chasing trends. They’re about quietly recognizing something that works, testing it seriously, and integrating it so that other developers can benefit without having to do that work themselves. That’s the honest story behind