DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

PHP Function To Delete A Non Empty Directory

02.18.2010
| 5994 views |
  • submit to reddit
        // 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);
		}