read-write-ini-in-php

Read and Update Config file (INI File) 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)

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

(readini.php)

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

(readupdate.php)

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

 

About the Author

agurchand

Software Engineer, Blogger etc., Visit my Youtube channel - <a href="http://www.youtube.com/agurchand">Click Here</a>

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

    • Author gravatar

      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

    • Author gravatar

      Hi Agurchand,
      I found where my problem is:
      I have a number of key / values as follows:
      vmon1=http://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.

    • Author gravatar

      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?

    • Author gravatar

      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

Your email address will not be published. Required fields are marked *