I want to delete a main folder along with all the files and subfolders inside it. I have tried a code block that uses glob()
to iterate over all files and then calls rmdir()
to delete the main folder and unlink()
to delete the files. However, the subfolders are not getting deleted. How can I modify this code to also delete the subfolders?
Example code:
foreach (glob($dir . '/*') as $file) {
if (is_dir($file)) {
rmdir($file);
} else {
unlink($file);
}
}
rmdir($dir);