Read and Update Config file (INI File) in PHP

read-write-ini-in-php

To parse / read INI file in PHP is very simple, there is a default function called parse_ini_file() which will read and return all the values as an array. 

To update or edit the INI file through PHP is little difficult, you cannot simply update the exact value in the INI, instead you have to re-write the entire INI file.

First, let’s look at an example INI file:

(config.ini)

[user_details]
username=agurchand
email=test@test.com
website=www.tutorialsmade.com

[other_websites]
web1=agurchand.com
web2=theonlytutorials.com

The below script will read the INI file and print it on the page.

(readini.php)

<?php 

	//put the file path here
	$filepath = 'config.ini';

	//parse the ini file using default parse_ini_file() PHP function
	$parsed_ini = parse_ini_file($filepath, true);

	//read the array and print
	foreach($parsed_ini as $section=>$values){
		echo "<h3>$section</h3>";
		foreach($values as $key=>$value){
			echo $key."=".$value."<br>";
		}
		echo "<br>";
	}

?>

Let’s jump to the script which will read as well write the INI file.

(readupdate.php)

<?php 

//put the file path here
$filepath = 'config.ini';

//after the form submit

if($_POST){
	$data = $_POST;
	//update ini file, call function
	update_ini_file($data, $filepath);
}

//this is the function going to update your ini file
	function update_ini_file($data, $filepath) { 
		$content = ""; 
		
		//parse the ini file to get the sections
		//parse the ini file using default parse_ini_file() PHP function
		$parsed_ini = parse_ini_file($filepath, true);
		
		foreach($data as $section=>$values){
			//append the section 
			$content .= "[".$section."]n"; 
			//append the values
			foreach($values as $key=>$value){
				$content .= $key."=".$value."n"; 
			}
		}
		
		//write it into file
		if (!$handle = fopen($filepath, 'w')) { 
			return false; 
		}

		$success = fwrite($handle, $content);
		fclose($handle); 

		return $success; 
	}
?>
<html>
<body>
<?php 

//parse the ini file using default parse_ini_file() PHP function
$parsed_ini = parse_ini_file($filepath, true);

?>

<form action="" method="post">
	<?php 
		
	foreach($parsed_ini as $section=>$values){
		echo "<h3>$section</h3>";
		//keep the section as hidden text so we can update once the form submitted
		echo "<input type='hidden' value='$section' name='$section' />";
		//print all other values as input fields, so can edit. 
		//note the name='' attribute it has both section and key
		foreach($values as $key=>$value){
			echo "<p>".$key.": <input type='text' name='{$section}[$key]' value='$value' />"."</p>";
		}
		echo "<br>";
	}

	?>
	<input type="submit" value="Update INI" />
</form>
<a target="_blank" href="config.ini">Open Config File</a>
</body>
</html>

Read the comments in the above file to learn how the script works.

See the DEMO or Download the script to use it in your project.

demo download

 

5 thoughts on “Read and Update Config file (INI File) in PHP

  1. Hi Agurchand,
    I am trying to use your ini solution on a raspberry pi but unfortunately I am having problems and believe it might be file permissions.

    I am using apache as the web server. Rather than copy the files into pi, I created new files using sudo nano and copied and pasted the code.
    checked with ls -l
    -rwxrwxrwx 1 www-data www-data 1726 Apr 5 21:37 parse_ini.php
    -rwxrwxrwx 1 www-data www-data 1717 Apr 5 21:43 gmconfig.ini

    My ini file has 5 sections and each key/value is as follows key=value (no spaces)
    Also I have no empty lines.

    Not sure where my problem is

  2. Hi Agurchand,
    I found where my problem is:
    I have a number of key / values as follows:
    vmon1=https://192.168.20.21/goform/motiondetect?cmd=set&md.active.enable=1′, auth=(‘admin’,’admin’)
    I get an error because of the = in the middle of my value.
    I have to wrap the value in ” ” and then your php script works when reading but when I click on update ini, the quotes I had in my config.ini are removed.

  3. Hey your tuoairtls are great..Are you going to do any other tuoairtls besides forums? I would like 1 where users can chat like a chat room.I tried going through the tuoairtls and always something comes up while I am doing it, can you post the full source for all the files please?

  4. Great job! It worked as intented but you have a little type mistake in (readupdate.php):

    Line 24: change “]n”; for “]\n”;
    Line 27: change “n”; for “\n”;

    You forgot to put those ‘\’ in order to make the line break (‘\n’).

    Very good job really, it helped me A LOT.

Leave a Reply

Theme: Overlay by Kaira
Agurchand