php8.5
Home/ Manual/ spl / directoryiterator/ DirectoryIterator::__construct

DirectoryIterator::__construct

PHP function Edit on GitHub ✎

(PHP 5, PHP 7, PHP 8)

Constructs a new directory iterator from a path

Description

DirectoryIterator::__construct(string $directory)

Constructs a new directory iterator from a path.

Parameters

directory

The path of the directory to traverse.

Errors/Exceptions

Throws an UnexpectedValueException if the directory does not exist.

Throws a ValueError if the directory is an empty string.

Changelog

VersionDescription
8.0.0Now throws a ValueError if directory is an empty string; previously it threw a RuntimeException.

Examples

A DirectoryIterator::__construct example

This example will list the contents of the directory containing the script.

php
<?php
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo) {
    if (!$fileinfo->isDot()) {
        var_dump($fileinfo->getFilename());
    }
}
?>

See Also

  • SplFileInfo
  • Iterator

Source: reference/spl/directoryiterator/construct.xml · from the official PHP manual (php/doc-en)