php.net |  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
Votes:8
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
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:
17 - 7 = ?
Subscribe to this entry?

 
 [2011-08-20 19:56 UTC] php at tracking-celebs dot info
Description:
------------
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:
opendir($foo);
without calling closedir() you get the same error, hence why I suggested it might be a handle not (properly) released or something.

Test script:
---------------
<?php
$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) { }
unset($iterator);
if (!rmdir($foo)) die("unable to delete $foo");

var_dump(file_exists($foo));

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

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


Expected result:
----------------
bool(false)

Actual result:
--------------
bool(false)
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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-08-22 13:58 UTC] kalle@php.net
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] kalle@php.net
-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
Description:
------------

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), 
                  RecursiveIteratorIterator::CHILD_FIRST);

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

rmdir($path); // Delete this Folder


// Calculate root folder size 
$size = 0
$Recursive2 = new RecursiveIteratorIterator(
		  new RecursiveDirectoryIterator($rootPath, 
                  RecursiveDirectoryIterator::SKIP_DOTS),
		  RecursiveIteratorIterator::SELF_FIRST
		);

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

[1] <http://php.net/supported-versions.php>
 [2018-09-02 09:34 UTC] cmb@php.net
-Status: Feedback +Status: No Feedback
 [2018-09-02 09:34 UTC] cmb@php.net
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-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 06:01:29 2024 UTC