Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP write_ini_file implementation

Below is an implementation of write_ini_file() which PHP is currently lacking, it will create an almost identical (except comments) of the input:

  • Supports cross platform (PHP_EOL) new lines added between sections.
  • Handles both index and key value arrays.
  • Handles CONSTANT style values.
  • And file locking to stay consistent.

Source

<?php
if (!function_exists('write_ini_file')) {
    /**
     * Write an ini configuration file
     * 
     * @param string $file
     * @param array  $array
     * @return bool
     */
    function write_ini_file($file, $array = []) {
        // check first argument is string
        if (!is_string($file)) {
            throw new \InvalidArgumentException('Function argument 1 must be a string.');
        }
        
        // check second argument is array
        if (!is_array($array)) {
            throw new \InvalidArgumentException('Function argument 2 must be an array.');
        }
        
        // process array
        $data = array();
        foreach ($array as $key => $val) {
            if (is_array($val)) {
                $data[] = "[$key]";
                foreach ($val as $skey => $sval) {
                    if (is_array($sval)) {
                        foreach ($sval as $_skey => $_sval) {
                            if (is_numeric($_skey)) {
                                $data[] = $skey.'[] = '.(is_numeric($_sval) ? $_sval : (ctype_upper($_sval) ? $_sval : '"'.$_sval.'"'));
                            } else {
                                $data[] = $skey.'['.$_skey.'] = '.(is_numeric($_sval) ? $_sval : (ctype_upper($_sval) ? $_sval : '"'.$_sval.'"'));
                            }
                        }
                    } else {
                        $data[] = $skey.' = '.(is_numeric($sval) ? $sval : (ctype_upper($sval) ? $sval : '"'.$sval.'"'));
                    }
                }
            } else {
                $data[] = $key.' = '.(is_numeric($val) ? $val : (ctype_upper($val) ? $val : '"'.$val.'"'));
            }
            // empty line
            $data[] = null;
        }
    
        // open file pointer, init flock options
        $fp = fopen($file, 'w');
        $retries = 0;
        $max_retries = 100;
    
        if (!$fp) {
            return false;
        }
    
        // loop until get lock, or reach max retries
        do {
            if ($retries > 0) {
                usleep(rand(1, 5000));
            }
            $retries += 1;
        } while (!flock($fp, LOCK_EX) && $retries <= $max_retries);
    
        // couldn't get the lock
        if ($retries == $max_retries) {
            return false;
        }
    
        // got lock, write data
        fwrite($fp, implode(PHP_EOL, $data).PHP_EOL);
    
        // release lock
        flock($fp, LOCK_UN);
        fclose($fp);
    
        return true;
    }
}

Example input .ini file (taken from http://php.net/manual/en/function.parse-ini-file.php)

; This is a sample configuration file
; Comments start with ';', as in php.ini

[first_section]
one = 1
five = 5
animal = BIRD

[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"

[third_section]
phpversion[] = "5.0"
phpversion[] = "5.1"
phpversion[] = "5.2"
phpversion[] = "5.3"

urls[svn] = "http://svn.php.net"
urls[git] = "http://git.php.net"

Example usage:

// load ini file values into array
$config = parse_ini_file('config.ini', true);

// add some additional values
$config['main']['foobar'] = 'baz';
$config['main']['const']['a'] = 'UPPERCASE';
$config['main']['const']['b'] = 'UPPER_CASE WITH SPACE';
$config['main']['array'][] = 'Some Value';
$config['main']['array'][] = 'ADD';
$config['third_section']['urls']['docs'] = 'http://php.net';

// write ini file
write_ini_file('config.ini', $config);

Resulting .ini file:

[first_section]
one = 1
five = 5
animal = BIRD

[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"

[third_section]
phpversion[] = 5.0
phpversion[] = 5.1
phpversion[] = 5.2
phpversion[] = 5.3
urls[svn] = "http://svn.php.net"
urls[git] = "http://git.php.net"
urls[docs] = "http://php.net"

[main]
foobar = "baz"
const[a] = UPPERCASE
const[b] = "UPPER_CASE WITH SPACE"
array[] = "Some Value"
array[] = ADD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.