php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #75135 Elements of DirectoryIterator is the iterator itself
Submitted: 2017-08-29 17:38 UTC Modified: 2021-08-19 14:31 UTC
From: morozov at tut dot by Assigned:
Status: Verified Package: SPL related
PHP Version: 7.1.8 OS: Linux
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: morozov at tut dot by
New email:
PHP Version: OS:

 

 [2017-08-29 17:38 UTC] morozov at tut dot by
Description:
------------
When iterating over a DirectoryIterator, each element is the iterator itself representing the current file. It makes it impossible to convert the iterator to array, as all its elements will be references to the same object (the iterator).

See https://3v4l.org/P0H06.

Test script:
---------------
$it = new DirectoryIterator('/tmp');
$array = iterator_to_array($it);

foreach ($array as $fileInfo) {
    var_dump($fileInfo->getRealPath());
}

/*

Outputs:

bool(false)
bool(false)

*/

Expected result:
----------------
The expected result is to see real paths of the files under the /tmp directory. It works if the iterator is iterated directly, without being converted to an array first.

Actual result:
--------------
The actual result is all real paths are FALSE since after having been converted to an array, the directory iterator is in an invalid state.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-05-12 13:15 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 [2021-05-12 13:15 UTC] cmb@php.net
In my opinion, DirectoryIterator is badly designed; it should not
extend SplFileInfo, but rather yield SplFileInfo instances on
iteration.  However, this is not something that can fixed at this
point in time, or at the very least that would require careful
consideration and the RFC process[1].

The behavior should, however, be explicitly mentioned in the docs.

[1] <https://wiki.php.net/rfc/howto>
 [2021-05-12 15:09 UTC] phpbugs at notanemail dot org
> In my opinion, DirectoryIterator is badly designed; it should not
> extend SplFileInfo, but rather yield SplFileInfo instances on
> iteration. 

FilesystemIterator was introduced, in PHP 5.3.0, whose default behaviour is to use SplFileInfo instances as the "current" values.

DirectoryIterator should have been deprecated at that time. No-one should be using DirectoryIterator: rather FilesystemIterator (or RecursiveDirectoryIterator, which extends FilesystemIterator).
 [2021-08-19 14:31 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2021-08-19 14:31 UTC] cmb@php.net
FilesystemIterator extends DirectoryIterator and has the very same
issue, namely that it iterates over itself.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 17:01:31 2024 UTC