php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44964 DirectoryIterator assignment within foreach()
Submitted: 2008-05-10 19:25 UTC Modified: 2008-05-11 20:28 UTC
From: phpbugs at rjharrison dot org Assigned: colder (profile)
Status: Not a bug Package: SPL related
PHP Version: 5.2.6 OS: Linux (fedora core 6 i think)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phpbugs at rjharrison dot org
New email:
PHP Version: OS:

 

 [2008-05-10 19:25 UTC] phpbugs at rjharrison dot org
Description:
------------
When looping over a $directorIterator using FOREACH something goes wrong when assigning the "as" variable to an array.




Reproduce code:
---------------
$filePath   = 'PUT YOUR TEST PATH HERE';
$dir        = new DirectoryIterator($filePath);
$files      = array();

foreach ($dir as $file){
    $files[] = $file;
    echo $file->getFilename(), " / ", $file->getSize(), " bytes\n";
}

echo "\nSecond loop:-\n";

foreach ($files as $file2){
    echo $file2->getFilename(), " / ", $file2->getSize(), " bytes\n";
}


Expected result:
----------------
(Depends on the files but assuming a single file in the directory)

test.php / 18 bytes
. / 4096 bytes
.. / 4096 bytes

Second run:-
test.php / 18 bytes
. / 4096 bytes
.. / 4096 bytes

Actual result:
--------------
test.php / 18 bytes
. / 4096 bytes
.. / 4096 bytes

Second run:-
 / 4096 bytes
 / 4096 bytes
 / 4096 bytes

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-11 18:10 UTC] colder@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

Even if it's odd, this is actually expected, DirectoryIterator::current() returns itself, so you're storing the same iterator multiple times in your array, consider either using clone or fetching directly the informations in your foreach.
 [2008-05-11 20:28 UTC] phpbugs at rjharrison dot org
Hi Colder,

I did consider cloning the object, but the behaviour is still wrong (imo):-

// $files[] = $file; change to ->
$files[] = clone $file;


Actual result:
--------------
test.php / 18 bytes
. / 4096 bytes
.. / 4096 bytes

Second loop:-
test.php / 18 bytes
test.php / 18 bytes
test.php / 18 bytes


Also, I disagree that original report expected behaviour (from a PHP User's perspective), even if it makes sense from an SPL perspective.

DirectoryIterator::current() returning _itself_ seems pretty crazy to me? Why doesn't it return an SplFileInfo object, representing the file in question?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 06:01:30 2024 UTC