Alex noticed, within the firm’s codebase, a technique referred to as recursive_readdir
. It had no feedback, however the identify appeared fairly clear: it could learn directories recursively, presumably enumerating their contents.
Happily for Alex, they checked the code earlier than blindly calling the strategy.
public operate recursive_readdir($path)
{
$deal with = opendir($path);
whereas (($file = readdir($deal with)) !== false)
{
if ($file != '.' && $file != '..')
{
$filepath = $path . '/' . $file;
if (is_dir($filepath))
{
rmdir($filepath);
recursive_readdir($filepath);
}
else
{
unlink($filepath);
}
}
}
closedir($deal with);
rmdir($path);
}
It is a recursive delete. rmdir
requires the goal listing to be empty, so this recurses over all of the information and subfolders within the listing, deleting them, in order that we will delete the listing.
This code is clearly cribbed from feedback on the PHP documentation, with a enjoyable distinction in that this model is each unclearly named, and in addition throws an additional rmdir
name within the is_dir
branch- a possible “optimization” that does not truly do something (it both fails as a result of the listing is not empty, or we find yourself calling it twice anyway).
Alex realized to take nothing as a right on this code base.
