How to remove (PHP)

If you want to delete a directory using PHP,  first you need to delete all existing files inside it.

For these purposes, you can use this function:

public function removeDirectory($dir): bool
{
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? $this->removeDirectory("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}

Comments

No comments yet, you can leave yours: