I have spent ages tracking down a problem to my DirectoryIterator usage.
I want to delete a directory so am first using DirectoryIterator to check if the directory contains any files, and if it does, I delete them and then try to delete the directory.
The trouble is, the rmdir command errors with a "permission denied" message. However if I don't include the line
$files = new DirectoryIterator($image_dir);
then I can go an delete the directory no problem.
Its as if the DirectoryIterator is hogging the directory so I can't delete it. Anyone know how to force the iterator to realease the directory? I am working in WIndows and have already tried closedir(opendir($image_dir)) before rmdir($image_dir) but it makes no different.
Thanks.
Annoying DirectoryIterator problem
Moderator: General Moderators
-
bobthebuilder
- Forum Commoner
- Posts: 32
- Joined: Tue Mar 22, 2011 5:06 pm
-
bobthebuilder
- Forum Commoner
- Posts: 32
- Joined: Tue Mar 22, 2011 5:06 pm
Re: Annoying DirectoryIterator problem
I gave up trying to use a DirectoryIterator, and used the following code instead:
The key to this working is the closedir($handle) line, as without that it didn't work. I couldn't work out what the equivalent construct is to release the DirectoryIterator. I hope this is of use to someone.
Code: Select all
$result = false;
if(is_dir($dir_path)) {
$handle = opendir($dir_path);
while (false !== ($file = readdir($handle))) {
if(!is_dir($file)) {
unlink($dir_path . "/" . $file);
}
}
closedir($handle);
$result = rmdir($dir_path);
}