php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54246 DirectoryIterator object loses information once copied into a variable
Submitted: 2011-03-14 13:48 UTC Modified: 2011-03-14 14:39 UTC
From: ebert at woltlab dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.2.17 OS: Windows 7 x64 (7601 w/ SP1)
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: ebert at woltlab dot com
New email:
PHP Version: OS:

 

 [2011-03-14 13:48 UTC] ebert at woltlab dot com
Description:
------------
Storing all DirectoryIterator objects fetched previously with an instance of DirectoryIterator within an array seems to make stored objects to lose all data.

While methods like 'isDir()' work fine in first loop, they will always fail within second loop.

---

C:\Program Files (x86)\PHP>php.exe -v
PHP 5.2.17 (cli) (built: Jan  6 2011 17:37:45)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies

---

 Verzeichnis von C:\inetpub\wwwroot\di

14.03.2011  13:33    <DIR>          .
14.03.2011  13:33    <DIR>          ..
14.03.2011  13:41               427 di.php
14.03.2011  13:33    <DIR>          directory
14.03.2011  13:33                 0 randomFile1.txt
14.03.2011  13:33             7.334 randomFile2.odt
14.03.2011  13:33                 0 randomFile3.bmp
               4 Datei(en),          7.761 Bytes
               3 Verzeichnis(se), 23.774.224.384 Bytes frei

Test script:
---------------
<?php
$array = array();
$dir = 'C:/inetpub/wwwroot/di/';

$it = new DirectoryIterator($dir);
foreach ($it as $obj) {
	if ($obj->isDot()) continue;
	var_dump($obj);
	if ($obj->isDir()) echo $obj->getFilename() . ' is a directory.<br />';
	else echo $obj->getFilename() . ' is a file<br />';
	
	$array[] = $obj;
}

echo '<hr />';

foreach ($array as $obj) {
	var_dump($obj);
	if ($obj->isDir()) echo $obj->getFilename() . ' is a directory.<br />';
	else echo $obj->getFilename() . ' is a file<br />';
}
?>

Actual result:
--------------
object(DirectoryIterator)#1 (0) { } di.php is a file
object(DirectoryIterator)#1 (0) { } directory is a directory.
object(DirectoryIterator)#1 (0) { } randomFile1.txt is a file
object(DirectoryIterator)#1 (0) { } randomFile2.odt is a file
object(DirectoryIterator)#1 (0) { } randomFile3.bmp is a file
(<hr />)
object(DirectoryIterator)#1 (0) { } is a directory.
object(DirectoryIterator)#1 (0) { } is a directory.
object(DirectoryIterator)#1 (0) { } is a directory.
object(DirectoryIterator)#1 (0) { } is a directory.
object(DirectoryIterator)#1 (0) { } is a directory.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-14 14:39 UTC] scottmac@php.net
-Status: Open +Status: Bogus
 [2011-03-14 14:39 UTC] scottmac@php.net
It's a reference to a single object and as it goes through the loop it's actually 
off the end making it empty.

If you want a copy use clone.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 17:01:29 2024 UTC