Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

RemoveDir //PHP Function

DZone's Guide to

RemoveDir //PHP Function

·
Free Resource
Removes a folder, including its subfolders and files in a efficient way without recursion, returns Boolean.


//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com

function removeFolder($dir){
	if(!is_dir($dir))
		return false;
	for($s = DIRECTORY_SEPARATOR, $stack = array($dir), $emptyDirs = array($dir); $dir = array_pop($stack);){
		if(!($handle = @dir($dir)))
			continue;
		while(false !== $item = $handle->read())
			$item != '.' && $item != '..' && (is_dir($path = $handle->path . $s . $item) ?
			array_push($stack, $path) && array_push($emptyDirs, $path) : unlink($path));
		$handle->close();
	}
	for($i = count($emptyDirs); $i--; rmdir($emptyDirs[$i]));
}
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}