Over a million developers have joined DZone.

PHP Function To Delete A Non Empty Directory

·
// PHP Function to delete a non empty directory


		function deleteDir($dir)
		{
		if ($handle = opendir($dir))
		{
		$array = array();
		
		while (false !== ($file = readdir($handle))) {
			if ($file != "." && $file != "..") {
		
				if(is_dir($dir.$file))
				{
					if(!@rmdir($dir.$file)) // Empty directory? Remove it
					{
					//deleteDir($dir.$file.'/'); // Not empty? Delete the files inside it
					}
				}
				else
				{
				   @unlink($dir.$file);
				}
			}
		}
		closedir($handle);
		
		@rmdir($dir);
		}

Topics:

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

{{ parent.tldr }}

{{ parent.urlSource.name }}