Location: PHPKode > scripts > INI handler > ini_handler.php
<?php
/* Class ini_handler
*  Reads, updates, writes new value, removes values in .ini files
*/
class ini_handler {

	/* function ini_read()
	*  Reads the value of a given key in a given .ini file
	*  Usage: ini_read('path of the file to read', 'name of the key which value have to be retrieved')
	*/
	function ini_read($path, $key)
	{
		
		// verify the path
		if(file_exists($path)):
		
			$lines = file($path, FILE_SKIP_EMPTY_LINES);
			
			foreach($lines as $val):
				$pos = strpos($val,'=');
				if($pos !== FALSE && trim(substr($val,0,$pos)) == $key && trim(substr($val,0)) != '#') $ret = trim(substr($val,$pos+1));
			endforeach;
			
		else:
		
			$ret = $path.' file not found.';
			
		endif;
	
		return $ret;
	
	}
	
	/* function ini_write()
	*  Updates the value of a given key in a given .ini file
	*  If the key do not exists, a new key and value are created
	*  Usage: ini_write('path of the file to read', 'name of the key which value have to be updated/writed', 'value to be written')
	*/
	function ini_write($path, $key, $to_write)
	{
		
		// verify the path
		if(file_exists($path)):
		
			$lines = file($path, FILE_SKIP_EMPTY_LINES);
			
			// flag to check if key is in the ini file found and modified
			$key_found = FALSE;
		
			foreach ($lines as $val):
				$pos = strpos($val,'=');
				if($pos !== FALSE && trim(substr($val,0,$pos)) == $key && trim(substr($val,0)) != '#'):
					// the key exists and is found+modified
					$key_found = TRUE;
					$line_new .= trim($key).'='.$to_write."\r\n";
				else:
					$line_new .= trim($val)."\r\n";
				endif;
			endforeach;
			
			$handle = fopen($path, "w");
			fwrite($handle,$line_new,strlen($line_new));
			fclose($handle);
			
			// if the key is not found a new key and value is appended
			if($key_found == FALSE):
				$line_new = trim($key).'='.$to_write."\r\n";
				$handle = fopen($path, "a");
				fwrite($handle,$line_new,strlen($line_new));
				fclose($handle);
			endif;
			
		else:
		
			echo($path.' file not found.');
			
		endif;
		
	}

	/* function ini_remove()
	*  Removes a line (key and value) in a given .ini file
	*  Usage: ini_remove('path of the file to read', 'name of the key to be removed')
	*/
	function ini_remove($path, $key)
	{
		
		// verify the path
		if(file_exists($path)):
		
			$lines = file($path, FILE_SKIP_EMPTY_LINES);
		
			foreach ($lines as $val):
				$pos = strpos($val,'=');
				if($pos !== FALSE && trim(substr($val,0,$pos)) == $key && trim(substr($val,0)) != '#'):
					// do nothing...easy!
				else:
					$line_new .= trim($val)."\r\n";
				endif;
			endforeach;
			
			$handle = fopen($path, "w");
			fwrite($handle,$line_new,strlen($line_new));
			fclose($handle);

		else:
		
			echo($path.' file not found.');
			
		endif;
		
	}

	/* function ini_dump()
	*  Dumps the content of a given .ini file
	*  Usage: ini_dump('path of the file to be dumped')
	*/
	function ini_dump($path)
	{
	
		// verify the path
		if(file_exists($path)):
		
			$lines = file($path, FILE_SKIP_EMPTY_LINES);
			foreach ($lines as $val):
				echo('<div>'.$val.'</div>');
			endforeach;
			
		else:
		
			echo($path.' file not found.');
			
		endif;		
		
	}
	
}
?>
Return current item: INI handler