I try to delete a big directory with a lot of subfolders and files (>1000). There are many functions built for this purpose, I use the following:
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir . "/" . $object) == "dir"){
log_message(201,array(),'Try to delete folder: '.$dir.'/'.$object);
rrmdir($dir . "/" . $object);
}else{
log_message(201,array(),'Try to delete FILE: '.$dir.'/'.$object);
unlink($dir . "/" . $object);
}
}
}
reset($objects);
rmdir($dir);
}
}
The problem is, that many files are left behind. Is this usual behavior, are is something wrong with my code? If it is usual behavior, how can I get around this problem?
Thanks in advance.
Different Operating handles this different. At most OS an filesystems, files can be locked exclusive for read or write operations.
If another process holds the file-handle with a lock, your process may not modify (or delete) the file. This may also be true for different threads.