php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49755 iterator_to_array nukes DirectoryIterator
Submitted: 2009-10-02 19:02 UTC Modified: 2009-10-06 13:50 UTC
Votes:6
Avg. Score:4.2 ± 0.9
Reproduced:6 of 6 (100.0%)
Same Version:1 (16.7%)
Same OS:2 (33.3%)
From: salathe@php.net Assigned: colder (profile)
Status: Wont fix Package: SPL related
PHP Version: 5.3.0 OS: OS X
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
29 + 12 = ?
Subscribe to this entry?

 
 [2009-10-02 19:02 UTC] salathe@php.net
Description:
------------
When converting a DirectoryIterator instance into an array using iterator_to_array, the array items appear to be converted to basic DirectoryIterator instances with none of the file information intact. The same does not appear to occur for other related classes (FilesystemIterator, RecursiveDirectoryIterator).

Reproduce code:
---------------
<?php

echo "DirectoryIterator:\n";
$dit = new DirectoryIterator(dirname(__FILE__));
$ait = iterator_to_array($dit);
foreach ($ait as $file) {
	var_dump($file->getFilename());
}

echo "RecursiveDirectoryIterator:\n";
$rdit = new RecursiveDirectoryIterator(dirname(__FILE__));
$ait = iterator_to_array($rdit);
foreach ($ait as $file) {
	var_dump($file->getFilename());
}

?>

Expected result:
----------------
DirectoryIterator:
string(1) "."
string(2) ".."
string(7) "bug.php"
RecursiveDirectoryIterator:
string(7) "bug.php"


Actual result:
--------------
DirectoryIterator:
string(0) ""
string(0) ""
string(0) ""
RecursiveDirectoryIterator:
string(7) "bug.php"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-02 21:09 UTC] fa@php.net
Not 5.3.0 only, also reproducable on a
"PHP 5.2.6-1+lenny3 with Suhosin-Patch 0.9.6.2"
 [2009-10-06 13:50 UTC] colder@php.net
This is, sadly, expected. DirectoryIterator is a weird iterator, first of all, it implements SplFileInfo, and, instead of returning new instances of SplFileInfo on current(), it returns itself.

So, if you store each value in an array, they will all be the same DirectoryIterator object, pointing to beyond the last element. The only solution is to clone it while puting it in an array, but iterator_to_array doesn't clone (and shouldn't clone).

You can similarly reproduce it with:

$dit = new DirectoryIterator(dirname(__FILE__));
$arr = array();
foreach ($ait as $file) {
    $arr[] = $file;
}

foreach($arr as $file) {
    var_dump($file->getFileName());
}


 [2014-01-11 10:17 UTC] dr dot dimitru at gmail dot com
Reproduced on Debian (Jessie) | PHP 5.5.7-2
 [2015-09-09 23:27 UTC] kevin dot waterson at gmail dot com
Reproduced with PHP 5.5.9-1ubuntu4.11 (cli) (built: Jul  2 2015 15:23:08)

<?php

        $iterator = new directoryIterator('./');
        $array =  iterator_to_array( $iterator, false );
        rsort( $array );
        print_r( $array );
?>

Produces an array of DirectoryIterators
php arr.php 
Array
(
    [0] => DirectoryIterator Object
        (
            [pathName:SplFileInfo:private] => 
            [glob:DirectoryIterator:private] => 
            [subPathName:RecursiveDirectoryIterator:private] => 
        )

    [1] => DirectoryIterator Object
        (
            [pathName:SplFileInfo:private] => 
            [glob:DirectoryIterator:private] => 
            [subPathName:RecursiveDirectoryIterator:private] => 
        )
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Jan 25 01:03:34 2022 UTC