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…

February 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

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

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

One thought

  1. 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…

February 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…

February 1, 2010

Is Empty Dir?

Php function that return true if the directory specified is empty.

November 9, 2009

Count coloured cells on Google Sheets with reference

In a spreadsheet if you need to count cells based on their background you can use this code in Google…

May 22, 2020

WP doesn’t send email? try this

Snippets for sending emails with Wordpress

February 8, 2020

Two post types share the same taxonomy in WordPress

Sharing a WordPress taxonomy between two post types is useful especially if you want to create a relation between different…

November 14, 2019