web analytics

How to read and write INI files in PHP?

INI Files

INI files are simple text files with a basic structure consisting of sections, property and value. The INI files can be used across different platform which is a great advantage as a single INI file can be used in various versions of the same application for various platforms.

INI files are used to store configurations for applications in a manner of property = value. Values can be single or one dimensional array. Also sections can be used to group same type of configurations. The last thing is comments, we can write comments in INI files. Comments in INI files starts with ; (semicolon)

Sample INI File

Following is a sample INI files which has comments, sections, property and values.

; this is our configuration file for the app that doesn't even exist :P

[App Config]

name = "Unborn Application"
version = "1.0.C"

[Authentication]

allow registration = 1
allow facebook_registration = 1
allow gmail_registration = 0

[Commenting]

allow commenting = 1
allow anonymous = "some data here"

[paths]

paths[root] = '/'
paths[uploads] = "/contents/uploads"

Syntax for INI Files

  1. Use semicolon (;) for comments
  2. Section names must be inside square brackets []
  3. Each property should start at new line followed by an equal which is followed by value of the property. Such as, property = value
  4. String values of properties should be inside single or double quotes
  5. Numeric values should not be inside quotes
  6. Arrays can be indexed; such as paths[root] or non-indexed such as paths[]. If non-indexed, numeric indexes will be placed in result as usual
  7. Only supports one dimensional arrays

Reading an INI File in PHP

Okay so to read this INI file in PHP, just make a call to the function parse_ini_file() as follows  

<?php
$config = parse_ini_file("config.ini", true);

Here, the first parameter is the path to the INI files and the second parameter indicates to include sections in the output which is by default false. However the output of the $config array would like following  

Array
(
    [App Config] => Array
        (
            [name] => "Unborn Application"
            [version] => "1.0.C"
        )

    [Authentication] => Array
        (
            [allow registration] => 1
            [allow facebook_registration] => 1
            [allow gmail_registration] => 0
        )

    [Commenting] => Array
        (
            [allow commenting] => 1
            [allow anonymous] => "some data here"
        )

    [paths] => Array
        (
            [paths] => Array
                (
                    [root] => "/"
                    [uploads] => "/contents/uploads"
                )

        )

)

If we do not want to use sections in output, then we should not send the second parameter.   Anyway, reading is simple but sadly there is no direct function from PHP to modify an existing configuration in an existing INI file or creating a whole new INI file.   But no worry, along with the following one there are so many custom functions to write or update configurations in INI file from PHP  

Editing or Updating an INI file in PHP

The following function consider that you have already parsed configuration from the INI file and now you have the configuration in an array. You modify the configuration array as you want then you send the array to the function to update or rewrite the INI file with the populated configuration array.

function put_ini_file($config, $file, $has_section = false, $write_to_file = true){
	$fileContent = '';
	if(!empty($config)){
		foreach($config as $i=>$v){
			if($has_section){
				$fileContent .= "[$i]".PHP_EOL.put_ini_file($v, $file, false, false);
			}
			else{
				if(is_array($v)){
					foreach($v as $t=>$m){
						$fileContent .= "$i[$t] = ".(is_numeric($m) ? $m : '"'.$m.'"').PHP_EOL;
					}
				}
				else $fileContent .= "$i = ".(is_numeric($v) ? $v : '"'.$v.'"').PHP_EOL;
			}
		}
	}

	if($write_to_file && strlen($fileContent)) return file_put_contents($file, $fileContent, LOCK_EX);
	else return $fileContent;
}

So, you call the above function like put_ini_file($configArray, “path/to/config.ini”, true);

However, you don’t send the 3rd parameter as true if you have no section in your configuration in INI, just send the first two parameters.

Please follow and like us:
Pin Share
RSS
Follow by Email
Scroll to Top