php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69003 RecursiveIteratorIterator breaks after removing directories on disk
Submitted: 2015-02-07 08:00 UTC Modified: 2015-02-07 10:19 UTC
From: elproducer20 at gmail dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: Irrelevant OS: Ubuntu14, Debian7
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: elproducer20 at gmail dot com
New email:
PHP Version: OS:

 

 [2015-02-07 08:00 UTC] elproducer20 at gmail dot com
Description:
------------
I think this is similar to this bug - https://bugs.php.net/bug.php?id=55468 except I'm trying to delete directories but not sure why this happens. If i remove the line "rmdir(...)" then script completes successfully printing all empty directory names to output. Once the line "rmdir(...)" is added back, it breaks with the below stack trace.

I worked around this by saving all directory names, then removing directories after the foreach loop completed.

Test script:
---------------
function deleteAllEmptyUserDirectories($path){

	$iterator = new RecursiveDirectoryIterator($path);

	$iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);

	// Get directories only
	$directories = new ParentIterator($iterator);

	// Loop over directories and remove empty ones
	foreach (new RecursiveIteratorIterator($directories, RecursiveIteratorIterator::SELF_FIRST) as $dir) {
	    // Count the number of "children" from the main directory iterator
	    if (iterator_count($iterator->getChildren()) === 0) {
	        rmdir($dir->getPathname());
	    	echo "[ DELETE ] " . $dir->getPathname() . "\n";
	    }
	}
}

deleteAllEmptyUserDirectories("/home/me/somedir/content");

Expected result:
----------------
[ DELETE ] 0 | /home/me/somedir/content/test
[ DELETE ] 0 | /home/me/somedir/content/test2


Actual result:
--------------
[ DELETE ] 0 | /home/me/somedir/content/test
PHP Fatal error:  Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(/home/me/somedir/content/test): failed to open dir: No such file or directory' in /home/me/www/scripts/deleteAllEmptyUserDirectories.php:18
Stack trace:
#0 [internal function]: RecursiveDirectoryIterator->__construct('/home/me...', 4096)
#1 [internal function]: RecursiveDirectoryIterator->getChildren()
#2 /home/me/www/scripts/deleteAllEmptyUserDirectories.php(18): RecursiveFilterIterator->getChildren()
#3 /home/me/www/scripts/deleteAllEmptyUserDirectories.php(27): deleteAllEmptyUserDirectories('/home/me...')
#4 {main}
  thrown in /home/me/www/scripts/deleteAllEmptyUserDirectories.php on line 18


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-07 10:19 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-02-07 10:19 UTC] requinix@php.net
You're recursing SELF_FIRST: the parent will be examined before its children are retrieved. If you delete the parent then PHP won't be able to get the children.

Go CHILD_FIRST.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 15:01:36 2025 UTC