php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #64292 Missing documentation details for return value of DirectoryIterator::current()
Submitted: 2013-02-24 15:19 UTC Modified: 2013-02-27 04:03 UTC
From: maciej dot sz at gmail dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: maciej dot sz at gmail dot com
New email:
PHP Version: OS:

 

 [2013-02-24 15:19 UTC] maciej dot sz at gmail dot com
Description:
------------
Since DirectoryIterator::current() returns itself there is many confusion while using it. It can't for instance be used as an argument of recursive method.

Test script:
---------------
foreach ( new DirectoryIterator(__DIR__) as $Current ) {
    var_dump($Di === $Current);
}

bool(true)
bool(true)
bool(true)
...

Expected result:
----------------
In order to use DirectoryIterator object as a function parameter you MUST clone it:

function iterateDirRecursive(DirectoryIterator $Di)
{
    foreach ( $Di as $Item ) {
        $Current = clone $Item; // notice the clone here
        if ( $Current->isDir() && ! $Current->isDot() ) {
            iterateDirRecursive($Current);
        }
    }
}

iterateDirRecursive(new DirectoryIterator(__DIR__));

Actual result:
--------------
Otherwise it will fail with "out of memory" or "maximum nesting level reached" depending on your PHP compilation:

function iterateDirRecursive(DirectoryIterator $Di)
{
    foreach ( $Di as $Item ) {
        $Current = $Item; // clone is missing
        if ( $Current->isDir() && ! $Current->isDot() ) {
            iterateDirRecursive($Current);
        }
    }
}

iterateDirRecursive(new DirectoryIterator(__DIR__));

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-27 04:03 UTC] frozenfire@php.net
This is not a bug. For recursively iterating directory contents, use 
RecursiveDirectoryIterator. DirectoryIterator is intended to give you a single 
level of directory contents that you can iterate. The current item is not a way 
to reference and iterate an item's children.
 [2013-02-27 04:03 UTC] frozenfire@php.net
-Status: Open +Status: Not a bug
 [2013-04-02 15:30 UTC] maciej dot sz at gmail dot com
This was a "missing documentation" report, not a bug report. Could you please paste your response to the DirectoryIterator documentation as a note or something similar? Maybe here: http://www.php.net/manual/en/directoryiterator.current.php

It should be included in documentation, because:
a) This is a very common misconception as using DirectoryIterator recursively seems like a natural thing (and the documentation is not clear about this)
b) This mistake can easily go unnoticed until late stages in application lifecycle
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 15:00:02 2026 UTC