Feb 02 2010

Recursive remove directory (RMDIR) in PHP

Category: PhpGiulio Pons @ 4:46 pm

This small php function is a recursive remove directory that remove non empty dirs recursively. It enters every directory, removes every file starting from the given path.

function rmdir_recurse($path) {
	$path = rtrim($path, '/').'/';
	$handle = opendir($path);
	while(false !== ($file = readdir($handle))) {
		if($file != '.' and $file != '..' ) {
			$fullpath = $path.$file;
			if(is_dir($fullpath)) rmdir_recurse($fullpath); else unlink($fullpath);
		}
	}
	closedir($handle);
	rmdir($path);
}
Share

Related posts:

  1. Recursive chmod on files and directory
  2. Is Empty Dir?
  3. Mini gallery/slideshow with PHP and JQuery
  4. Calculate dir size recursively with PHP (and count files)
  5. Always remove slashes from GET and POST

Tags: , , ,

One Response to “Recursive remove directory (RMDIR) in PHP”

  1. Austin Shoulta says:

    What i don’t realize is if truth be told how you are now not really much more well-liked than you might be now. You are very intelligent. You know thus significantly in relation to this subject, produced me personally believe it from so many various angles. Its like men and women aren’t interested unless it’s something to accomplish with Girl gaga! Your individual stuffs outstanding. All the time handle it up!

Leave a Reply