Alex saw, in the company’s codebase, the so-called method recursive_readdir
. It had no comments, but the name seemed pretty clear: it will read directories recursively, presumably enumerating their contents.
Fortunately for Alex, they checked the code before blindly calling the method.
public function recursive_readdir($path)
$handle = opendir($path);
while (($file = readdir($handle)) !== false)
if ($file != '.' && $file != '..')
$filepath = $path . '/' . $file;
if (is_dir($filepath))
rmdir($filepath);
recursive_readdir($filepath);
else
unlink($filepath);
closedir($handle);
rmdir($path);
This is recursive deletion. rmdir
requires the target directory to be empty, so it iterates over all the files and subfolders in the directory, deleting them, so we can delete the directory.
This code is clearly compiled from comments on the PHP documentation, with the amusing difference that this version is vaguely named, and also provides an additional rmdir
invite to is_dir
branch – a potential “optimization” that doesn’t actually do anything (or fails because the directory isn’t empty, or we end up calling it twice anyway).
Alex learned to take nothing taken for granted in this code base.
BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Find out how!