$path = "/path/to/folder"; if (file_exists($path)) { removeFolder($path); echo "Folder and its contents have been deleted successfully."; } else { echo "Folder not found."; }
$dir = "/folder/to/remove"; if(is_dir($dir)){ $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { if (is_dir($dir."/".$object)) removeFolder($dir."/".$object); else unlink($dir."/".$object); } } rmdir($dir); }This example shows how to remove a folder and its content recursively using the removeFolder function. The removeFolder function is part of the PHP Filesystem package library, which is included in the PHP core. It does not need to be installed separately.