I'm trying to have my code delete a temporary directory that was generated earlier in the code. This directory contains multiple files and subdirectories, all of which are recursively removed before mkdir is called. however, rmdir generates an arror, claiming the directory isn't empty.
Scandir returns three values: ".", ".." and a generic folder that should've been removed earlier. However, file_exists returns false on this folder.
I have checked for hidden folders/files and I have checked permissions, but the error still occurs. Any ideas what might be causing this?
Code:
$clean=array();
foreach($files as $file){ // $files is a RecursiveIteratorIterator that worked fine earlier in the script
array_push($clean,$file);
}
// sort files by name length to start with the subdirectories and work towards the root
usort($clean,function($a,$b){
return strlen($b)-strlen($a);
});
foreach($clean as $file){
if(!is_dir($file)&&!preg_match('/\.{1,2}$/',$file)){
unlink($file);
}elseif(preg_match('/\.{2}$/',$file)){
rmdir(str_replace('\..','',$file));
}
}
rmdir($tmpdir);
This script is in my root folder.
The directory to be removed is: [root]\files\AV[generic name]
$tmpdir is the path to this directory and it works fine earlier in the code.
e.g. if the folder to be removed is [root]\files\AV\tmp then $tmpdir='files/AV/tmp/'