private static function getFileHandle($filename, $mode='c') {
		$write = in_array($mode,array('c','c+'));

		if($write) {
			//make sure the directory exists and is writable
			$directory = dirname($filename);
			if(!file_exists($directory)) {
				if(!mkdir($directory,0777,true)) {
					return false;
				}
			}
			elseif(!is_dir($directory)) {
				return false;
			}
			elseif(!is_writable($directory)) {
				return false;
			}
		}
		
		//get file pointer
		$fh = fopen($filename,$mode);

		if(!$fh) return false;

		//lock file with appropriate lock type
		if($write) {
			if(!flock($fh,LOCK_EX)) {
				self::closeFile($fh);
				return false;
			}	
		}
		else {
			if(!flock($fh,LOCK_SH)) {
				self::closeFile($fh);
				return false;
			}
		}
		
		return $fh;
	}