So I have this function I am using to delete files and subdirectories recursively. It can also delete single and an array of files, though that is not the issue. Everything works fine up until it has to delete the directories, all levels of files get deleted, yet folders will not. It will however delete the target directory if no subdirectories exist. Not sure if it's a directory handle issue, or what at this point. It is not a permissions issue although.
public static function delete($path, $files){
$dirHandle = opendir($path);
if($files === '*'){ //delete all files and subdirectories
foreach(glob("{$path}/*") as $file){
if(self::checkExist($file, 'folder')){
$pdp = dirname($file) . '/';
$file = basename($file);
self::delete($pdp, $file);
} else {
if(!unlink($file)){
//flash message: 'Sorry, something went wrong. Please contact an administrator.'
//log error couldnt delete file: $file
}
}
}
if(count(glob("{$path}/*.*", GLOB_BRACE)) === 0){
closedir($dirHandle);
if(!rmdir($path)){
//flash message: 'Sorry, something went wrong. Please contact an administrator.'
//log error couldnt remove directory: $path
}
}
return true;
}}
I also need to keep the checks and space for error logging, my entire Class, keeps this code 'theme' consistently.