Add Storage::deleteEmptyDirectory() to recursively clear empty directories

This commit is contained in:
Kijin Sung 2017-01-26 18:01:27 +09:00
parent 91ff3c6323
commit 3dc736817d
2 changed files with 52 additions and 0 deletions

View file

@ -714,6 +714,35 @@ class Storage
}
}
/**
* Delete a directory only if it is empty.
*
* @param string $dirname
* @param bool $delete_empty_parents (optional)
* @return bool
*/
public static function deleteEmptyDirectory($dirname, $delete_empty_parents = false)
{
if (!self::isDirectory($dirname) || !self::isEmptyDirectory($dirname))
{
return false;
}
$result = @rmdir($dirname);
if (!$result)
{
return false;
}
else
{
if ($delete_empty_parents)
{
self::deleteEmptyDirectory(dirname($dirname), true);
}
return true;
}
}
/**
* Get the current umask.
*