I'm trying to delete nested folders I generate after zipping/downloading the contents, but I'm running into an issue where I end up with an empty folder that can't be deleted. I found this answer in a similar question, and a bunch of versions of it, but for some reason the last folder thinks it is not empty despite having no contents.
function recursiveRemove($dir) {
$structure = glob(rtrim($dir, "/").'/*');
if (is_array($structure)) {
foreach($structure as $file) {
if (is_dir($file)) recursiveRemove($file);
elseif (is_file($file)) unlink($file);
}
}
rmdir($dir);
}
Any help would be appreciated!
It was trying to delete too many folders too quickly - having the system open/close the folder was enough for it to verify there was nothing inside and deleted everything as intended.
function recursiveRemove($dir) {
$structure = glob(rtrim($dir, "/").'/*');
if (is_array($structure)) {
foreach($structure as $file) {
if (is_dir($file)) recursiveRemove($file);
elseif (is_file($file)) unlink($file);
}
}
//Opening and closing the directory forces system to recognize it is empty
$openCheck = opendir($dir);
closedir($openCheck);
//Remove directory after done removing all children
rmdir($dir);
}
Thank you Xatenev for putting me on the right track! I feel like this might be a little janky - if anyone has a better idea please let me know!
Try this:
/**
* Remove directory recursively.
*
* @param string $path Path
* @return bool True on success or false on failure.
*/
function rrmdir($path) {
$files = glob($path . '/*');
foreach ($files as $file) {
is_dir($file) ? rrmdir($file) : unlink($file);
}
return rmdir($path);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.