Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

PHP Function to delete a non empty directory (See related posts)

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


You need to create an account or log in to post comments to this site.


Click here to browse all 7718 code snippets

Related Posts