|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55468 UnexpectedValueException caused by an unreleased handle or something
Submitted: 2011-08-20 19:56 UTC Modified: 2018-09-02 09:34 UTC
Avg. Score:4.1 ± 0.9
Reproduced:7 of 7 (100.0%)
Same Version:5 (71.4%)
Same OS:6 (85.7%)
From: php at tracking-celebs dot info Assigned: cmb (profile)
Status: No Feedback Package: SPL related
PHP Version: Irrelevant OS: win32
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2011-08-20 19:56 UTC] php at tracking-celebs dot info
After using a DirectoryIterator on a folder, then removing said folder, using another iterator to iterate on the folder's parent will see/try to look into the removed folder.

This only happens on Windows, so maybe due to a cache somewhere or something. Tried adding a clearstatcache() just in case, but that doesn't change anything.

FYI if you replace the first DirectoryIterator by a:
without calling closedir() you get the same error, hence why I suggested it might be a handle not (properly) released or something.

Test script:
$folder = 'V:\\tmp\\foobar';
if (file_exists($folder)) die("$folder already exists");
if (!mkdir($folder)) die("unable to create $folder");
$foo = $folder . '\\foo';
if (!mkdir($foo)) die("unable to create $foo");

$iterator = new DirectoryIterator($foo);
foreach ($iterator as $fi) { }
if (!rmdir($foo)) die("unable to delete $foo");


$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $fi) { }

if (!rmdir($folder)) die("unable to delete $folder");

Expected result:

Actual result:
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(V:\tmp\foobar\foo,V:\tmp\foobar\foo): Access is denied. (code: 5)' in V:\test\test.php on line 35


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-08-22 13:58 UTC]
At first glance it doesn't looks like a problem in PHP itself but more that you haven't granted yourself the permission to delete the folder on Windows.

Have you tried to chmod or similar it?
 [2011-08-22 13:59 UTC]
-Status: Open +Status: Feedback
 [2011-08-25 00:06 UTC] php at tracking-celebs dot info
-Status: Feedback +Status: Open
 [2011-08-25 00:06 UTC] php at tracking-celebs dot info
Permissions aren't a problem here.

Besides, the (first, $foo) folder actually gets removed (as indicated by the output of file_exists), that's not the problem.

And the problem comes before trying to remove the other one ($folder), it is that when calling the iterator (on that parent), because it'll somehow still "hold"/find a reference to the now non-existing/freshly removed folder, thus causing the exception...
 [2012-09-08 14:04 UTC] sephirot_germany at hotmail dot com

I have same problem, When I calculate size of root folder 
after delete sub folder error will come.
I think Iterator use old reference. 

Test script:

$rootPath = "C:\public/upload";
$path = "C:\public/upload/New Folder"; // Path to delete folder

$Recursive1 = new RecursiveIteratorIterator(
              new RecursiveDirectoryIterator($path), 

// Delete all subfolder
foreach ($Recursive1 as $object) {
  if ($object->isDir()) {
  } else {

rmdir($path); // Delete this Folder

// Calculate root folder size 
$size = 0
$Recursive2 = new RecursiveIteratorIterator(
		  new RecursiveDirectoryIterator($rootPath, 

// Error here
foreach($Recursive2 as $object){ 
  $size  += is_file($object->getPath()) ? filesize($object->getPath()):0;
 [2018-08-18 14:10 UTC]
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2018-08-18 14:10 UTC]
I can't reproduce the described behavior.  Can anybody else with
any actively supported PHP version[1]?

[1] <>
 [2018-09-02 09:34 UTC]
-Status: Feedback +Status: No Feedback
 [2018-09-02 09:34 UTC]
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed Aug 10 05:05:46 2022 UTC