Content · Programming · PHP

PHP DirectoryIterator::valid() Method: Usage and Examples

Checks whether the DirectoryIterator cursor currently points to an entry.

1 views Published Oct 17, 2026 Updated Oct 17, 2026

DirectoryIterator::valid() Method is one of the PHP operations used in filesystem and stream workflows. Checks whether the DirectoryIterator cursor currently points to an entry. The examples below show what it does and how to handle its result.

What Is DirectoryIterator::valid() Method?


Checks whether the DirectoryIterator cursor currently points to an entry.

Its syntax is:

DirectoryIterator::valid()

This function or method takes no parameters.

Return value: The declared return type is bool. valid() indicates iterator state, not whether the current entry is a regular file.

Important: valid() indicates iterator state, not whether the current entry is a regular file.

Example 1: Basic Usage


The basic example focuses on the behavior of DirectoryIterator::valid() Method: checks whether the DirectoryIterator cursor currently points to an entry. Inputs are defined locally or shown as application-controlled paths.

<?php

$entries = new DirectoryIterator(__DIR__);
while ($entries->valid()) {
    if (!$entries->isDot()) { echo $entries->getFilename() . "\n"; }
    $entries->next();
}

The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.

Example 2: A Different Use Case


This second example illustrates another input or execution condition for DirectoryIterator::valid() Method. Notice the different checks, arguments, or output handling.

<?php

$entries = new DirectoryIterator(__DIR__);
$entries->rewind();
if (!$entries->valid()) { echo "The directory has no entries"; }

This variation matters because valid() indicates iterator state, not whether the current entry is a regular file.

Practical Example: Using the Operation in an Application


The following example demonstrates how DirectoryIterator::valid() Method fits into a more complete file or archive workflow, including inputs and appropriate checks.

<?php

$iterator = new DirectoryIterator(__DIR__);
$regularFiles = 0;
for ($iterator->rewind(); $iterator->valid(); $iterator->next()) {
    if ($iterator->isFile()) { $regularFiles++; }
}
echo "Files: {$regularFiles}";

The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.

Important Behavior and Edge Cases


  • Function behavior: Checks whether the DirectoryIterator cursor currently points to an entry.
  • Limitation: valid() indicates iterator state, not whether the current entry is a regular file.

Comparison With Related Functions


Function or MethodDifference
DirectoryIterator::valid() MethodChecks whether the DirectoryIterator cursor currently points to an entry.
DirectoryIterator::current()Reads the current entry, whereas valid() reports only cursor validity.

Use DirectoryIterator::valid() Method for the specific behavior described above. The related operation, DirectoryIterator::current(), serves a different purpose; do not substitute it unless that different behavior is intended.

Common Beginner Mistakes


MistakeCorrect Approach
Confusing iterator position, keys and returned values.valid() indicates iterator state, not whether the current entry is a regular file. Verify cursor position and flags before reading or advancing entries.
Treating DirectoryIterator::current() as an interchangeable replacement for DirectoryIterator::valid() Method.Reads the current entry, whereas valid() reports only cursor validity.

Key Takeaways


  • Checks whether the DirectoryIterator cursor currently points to an entry.
  • valid() indicates iterator state, not whether the current entry is a regular file.
  • Related operation: DirectoryIterator::current() — Reads the current entry, whereas valid() reports only cursor validity.

DirectoryIterator::valid() Method is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.

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