Content · Programming · PHP

PHP readdir() Function: Usage and Examples

Learn PHP readdir() through clear code examples, practical usage, important limitations and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

readdir() reads the next name from an open directory handle. This guide explains the function with focused examples and the decisions that matter in real applications.

What Is readdir() in PHP?

Reads the next name from an open directory handle.

A typical call looks like this:

<?php

readdir([$dir_handle]);

The input arguments shown in the call identify the relevant connection, resource, value, path or configuration for the operation. Pay attention to argument order and to the return value demonstrated below.

Example 1: Basic Usage

This first example uses readdir() directly to demonstrate its basic purpose.

<?php

$directory = opendir(__DIR__); while (($entry = readdir($directory)) !== false) { echo $entry . PHP_EOL; } closedir($directory);

The returned value illustrates reads the next name from an open directory handle. The result includes . and .. on some systems; false indicates end of iteration.

Example 2: A Different Input or Option

The next example changes the input or option, showing another supported use of readdir().

<?php

$directory = opendir(__DIR__); while (($entry = readdir($directory)) !== false) { if ($entry === "." || $entry === "..") continue; echo $entry . PHP_EOL; } closedir($directory);

The returned value illustrates reads the next name from an open directory handle. The result includes . and .. on some systems; false indicates end of iteration.

Practical Example: Applying the Function

In application code, readdir() belongs in a controlled workflow with input validation, a checked result and cleanup where needed.

<?php

$directory = opendir(__DIR__); $files = []; while (($entry = readdir($directory)) !== false) { if (is_file(__DIR__ . DIRECTORY_SEPARATOR . $entry)) $files[] = $entry; } closedir($directory); print_r($files);

The returned value illustrates reads the next name from an open directory handle. The result includes . and .. on some systems; false indicates end of iteration.

Important Behavior and Edge Cases

  • Main behavior: Reads the next name from an open directory handle.
  • Important limitation: The result includes . and .. on some systems; false indicates end of iteration.
  • Requirements: Check the runtime, file permissions and operating system when results depend on the environment.

Comparison With Related Functions

Function or ApproachWhen to Use It
readdir()Reads the next name from an open directory handle.
scandirReads an entire directory listing into an array rather than returning one entry at a time.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the specific limits or preconditions of readdir().The result includes . and .. on some systems; false indicates end of iteration.
Assuming every environment produces the same result.Check the runtime, file permissions and operating system when results depend on the environment.

Key Takeaways

  • Reads the next name from an open directory handle.
  • The result includes . and .. on some systems; false indicates end of iteration.
  • Check the runtime, file permissions and operating system when results depend on the environment.

Use readdir() when its behavior matches your task, and verify the relevant return value before taking further action. The output depends on the local configuration when it uses resources, files or an interactive terminal.

Share this resourceSend this page to someone who may find it useful.