Recursive remove directory (RMDIR) in PHP

This small php function is a recursive remove directory that remove non empty dirs recursively. It enters every directory, removes…

Febbraio 2, 2010

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);
}

Author

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.

Comments on “Recursive remove directory (RMDIR) in PHP”

One thought

  1. Austin Shoulta ha detto:

    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!

Comments are closed

Recommended

ASP Function to count files in a folder

This ASP function counts files of a specified extension inside a folder: this is done by creating a file system…

Febbraio 9, 2010

Calculate dir size recursively with PHP (and count files)

This small PHP function lets you calculate the dir size entering each sub dir and making the sum of the…

Febbraio 1, 2010

Is Empty Dir?

Php function that return true if the directory specified is empty. $bool = is_emptydir("./images"); function is_emptydir($which){ $dh=dir($which); $emptydir=true; while ($file=$dh->read())…

Novembre 9, 2009

The Quantcast CMP broke my sites

A javascript error in CMP blocks my site.

Maggio 19, 2023

WP Gutenberg notes

Collection of notes and thoughts on Wordpress Gutenberg blocks development.

Gennaio 9, 2023