I'm using the following function to delete all files and subfolders from a folder, before removing the folder itself:
public function deleteDownload($dir): void
{
foreach(glob($dir . '/{,.}[!.,!..]*',GLOB_MARK|GLOB_BRACE) as $file) {
if(is_dir($file)) {
$this->deleteDownload($file);
}
else {
unlink($file);
}
}
rmdir($dir);
}
This works great, until the program stumbles upon a directory with braces in the name, e.g: {test}
. When this happens, the program throws an exception saying that the directory is not empty, which is correct as it contains files that should have been deleted:
ErrorException rmdir(/Users/xx/xx/xx/xx/xx/storage/app/c544946e914f/test//{test}/): Directory not empty
When I rename the filename from {test}
to test
everything works as expected.
Can someone explain to me why this happens and how to fix this?