<?php
class FileManager{
	function __construct(){
		
	}
	
	
	
	/**
	 * delete all files and folders in directory including it
	 * @params {string} $dirPath path to directory
	 */
	public static function deleteDir($dir) {
		//detect PHP verson
		$v =  phpversion();
		$vs = explode(".",$v);
		if(($vs[0] ==5 && $vs[1] >= 2) || $vs[0] >5){
			self::deleteDirBelow($dir);
		}		
		//delete all files and folders
		if(is_dir($dir) || file_exists($dir)){
			$it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
			$files = new RecursiveIteratorIterator($it,
						 RecursiveIteratorIterator::CHILD_FIRST);
			foreach($files as $file) {
				if ($file->isDir()){
					rmdir($file->getRealPath());
				} else {
					unlink($file->getRealPath());
				}
			}
			if(is_dir($dir)){
				rmdir($dir);
			}else{
				unlink($dir);
			}
		}
	}
	/**
	 * delete all files and folders in directory including it for PHP verson below 5.2
	 * @params {string} $dirPath path to directory
	 */
	public static function deleteDirBelow($dirPath) {
		//delete file if $dirPath is directory
		if (! is_dir($dirPath)) {
			if(file_exists($dirPath)){
				unlink($dirPath);
				return;
			}
		}
		//delete all file in directory
		if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
			$dirPath .= '/';
		}
		$files = glob($dirPath . '*', GLOB_MARK);
		foreach ($files as $file) {
			if (is_dir($file)) {
				self::deleteDirBelow($file);
			} else {
				unlink($file);
			}
		}
		if(is_dir($dirPath)){
			rmdir($dirPath);
		}
	}
	
	
	
}