parse_ini_file

(PHP 4, PHP 5, PHP 7)

parse_ini_fileОбрабатывает конфигурационный файл

Описание

array parse_ini_file ( string $filename [, bool $process_sections = false [, int $scanner_mode = INI_SCANNER_NORMAL ]] )

parse_ini_file() загружает ini-файл, указанный в аргументе filename, и возвращает его настройки в виде ассоциативного массива.

Структура ini-файла похожа на структуру php.ini.

Список параметров

filename

Имя обрабатываемого ini-файла.

process_sections

Установив параметр process_sections в TRUE, вы получаете многомерный массив, который включает как название отдельных настроек, так и секции. По умолчанию process_sections равен FALSE

scanner_mode

Может принимать следующие значения: INI_SCANNER_NORMAL (по умолчанию) или INI_SCANNER_RAW. Если указано значение INI_SCANNER_RAW, то значения опций не будут обрабатываться.

Возвращаемые значения

В случае успеха настройки возвращаются в виде ассоциативного array. В случае ошибки возвращается FALSE.

Список изменений

Версия Описание
5.3.0 Добавлен необязательный параметр scanner_mode. Одинарные кавычки теперь также могут быть использованы в присвоениях переменных. Символ решетки (#) теперь больше не может быть использован в качестве комментария и будет выбрасывать предупреждение о том, что данная возможность считается устаревшей.
5.2.7 В случае ошибки синтаксиса данная функция теперь вернет FALSE, а не пустой массив.
5.2.4 Ключи и имена секций, состоящие из цифр, будут обработаны в PHP как целые числа, поэтому числа, начинающиеся с 0 будут считаться восьмеричными, а начинающиеся с 0x - шестнадцатеричными.
5.0.0 Значения, заключенные в двойные кавычки, теперь могут содержать переводы строк.
4.2.1 На поведение этой функции теперь влияет безопасный режим и open_basedir.

Примеры

Пример #1 Содержимое sample.ini

; Это пример файла настроек
; Комментарии начинаются с ';', как в 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"

Пример #2 Пример использования функции parse_ini_file()

Константы также могут обрабатываться в ini-файлах, так что если вы объявите константу в виде значения для ini-файла до вызова parse_ini_file(), то константа будет корректно обработана. Таким образом обрабатываются только значения опций. Например:

<?php

define
('BIRD''Dodo bird');

// Обрабатываем без секций
$ini_array parse_ini_file("sample.ini");
print_r($ini_array);

// Обрабатываем с секциями
$ini_array parse_ini_file("sample.ini"true);
print_r($ini_array);

?>

Результатом выполнения данного примера будет что-то подобное:

Array
(
    [one] => 1
    [five] => 5
    [animal] => Dodo bird
    [path] => /usr/local/bin
    [URL] => http://www.example.com/~username
    [phpversion] => Array
        (
            [0] => 5.0
            [1] => 5.1
            [2] => 5.2
            [3] => 5.3
        )

)
Array
(
    [first_section] => Array
        (
            [one] => 1
            [five] => 5
            [animal] => Dodo bird
        )

    [second_section] => Array
        (
            [path] => /usr/local/bin
            [URL] => http://www.example.com/~username
        )

    [third_section] => Array
        (
            [phpversion] => Array
                (
                    [0] => 5.0
                    [1] => 5.1
                    [2] => 5.2
                    [3] => 5.3
                )

        )

)

Пример #3 Обработка php.ini файла функцией parse_ini_file()

<?php
// Простая функция для сравнения результатов
function yesno($expression)
{
    return(
$expression 'Yes' 'No');
}

// Получаем путь к php.ini с помощью функции php_ini_loaded_file()
// функция доступна начиная с версии PHP 5.2.4
$ini_path php_ini_loaded_file();

// Обрабатываем php.ini
$ini parse_ini_file($ini_path);

// Выводим и сравниваем значения, учтите, что использование get_cfg_var()
// даст одинаковые результаты для используемых здесь значений parsed (загруженное из файла) и loaded (используемое в данный момент)
echo '(parsed) magic_quotes_gpc = ' yesno($ini['magic_quotes_gpc']) . PHP_EOL;
echo 
'(loaded) magic_quotes_gpc = ' yesno(get_cfg_var('magic_quotes_gpc')) . PHP_EOL;
?>

Результатом выполнения данного примера будет что-то подобное:

(parsed) magic_quotes_gpc = Yes
(loaded) magic_quotes_gpc = Yes

Примечания

Замечание:

Эта функция не имеет никакого отношения к файлу php.ini. К моменту выполнения вашего скрипта, он уже обработан. Эта функция может быть использована для загрузки настроек вашего собственного приложения.

Замечание:

Если значение в ini-файле содержит прочие символы, кроме букв и цифр, оно должно заключаться в двойные кавычки (").

Замечание: Существует зарезервированные слова, которые нельзя использовать в качестве ключей в ini-файлах. Такими словами являются: null, yes, no, true, false, on, off, none. Значения null, off, no и false преобразуются в "". Значения on, yes и true преобразуются в "1". Символы ?{}|&~![()^" не должны использоваться в ключах и иметь какой-либо особый смысл в значениях.

Замечание:

Записи без знака равенства игнорируются. Например, "foo" игнорируется, тогда как "bar =" обрабатывается и добавляется с пустым значением. Например, в MySQL есть опция "no-auto-rehash", устанавливаемая в my.cnf, которая не имеет значения и игнорируется.

Смотрите также

Коментарии

Just a quick note for all those running into trouble escaping double quotes:

I got around this by "base64_encode()"-ing my content on the way in to the ini file, and "base64_decode()"-ing on the way out.

Because base64 uses the "=" sign, you will have to encapsulate the entire value in double quotes so the line looks like this:

    varname = "TmlhZ2FyYSBGYWxscywgT04="

When base64'd, your strings will retain all \n, \t...etc...  URL's retain everything perfectly :-)

I hope some of you find this useful!

Cheers, Kieran
2003-01-07 12:24:15
http://php5.kiev.ua/manual/ru/function.parse-ini-file.html
Автор:
This is a simple (but slightly hackish) way of avoiding the character limitations (in values):

<?php
define
('QUOTE''"');
$test parse_ini_file('test.ini');

echo 
"<pre>";
print_r($test);
?>

contents of test.ini:

park yesterday = "I (walked) | {to} " QUOTE"the"QUOTE " park yesterday & saw ~three~ dogs!"

output:

<?php
Array
(
    [
park yesterday] => (walked) | {to"the" park yesterday saw ~threedogs!
)
?>
2006-10-31 13:46:55
http://php5.kiev.ua/manual/ru/function.parse-ini-file.html
Warning: parse_ini_files cannot cope with values containing the equal sign (=).

The following function supports sections, comments, arrays, and key-value pairs outside of any section.
Beware that similar keys will overwrite one another (unless in different sections).

<?php
function parse_ini $filepath ) {
   
$ini file$filepath );
    if ( 
count$ini ) == ) { return array(); }
   
$sections = array();
   
$values = array();
   
$globals = array();
   
$i 0;
    foreach( 
$ini as $line ){
       
$line trim$line );
       
// Comments
       
if ( $line == '' || $line{0} == ';' ) { continue; }
       
// Sections
       
if ( $line{0} == '[' ) {
           
$sections[] = substr$line1, -);
           
$i++;
            continue;
        }
       
// Key-value pair
       
list( $key$value ) = explode'='$line);
       
$key trim$key );
       
$value trim$value );
        if ( 
$i == ) {
           
// Array values
           
if ( substr$line, -1) == '[]' ) {
               
$globals$key ][] = $value;
            } else {
               
$globals$key ] = $value;
            }
        } else {
           
// Array values
           
if ( substr$line, -1) == '[]' ) {
               
$values$i ][ $key ][] = $value;
            } else {
               
$values$i ][ $key ] = $value;
            }
        }
    }
    for( 
$j=0$j<$i$j++ ) {
       
$result$sections$j ] ] = $values$j ];
    }
    return 
$result $globals;
}
?>

Example usage:
<?php
$stores 
parse_ini('stores.ini');
print_r$stores );
?>

An example ini file:
<?php
/*
;Commented line start with ';'
global_value1 = a string value
global_value1 = another string value

; empty lines are discarded
[Section1]
key = value
; whitespace around keys and values is discarded too
otherkey=other value
otherkey=yet another value
; this key-value pair will overwrite the former.
*/
?>
2007-10-29 08:33:50
http://php5.kiev.ua/manual/ru/function.parse-ini-file.html
Here is a quick parse_ini_file wrapper to add extend support to save typing and redundancy.
<?php
   
/**
     * Parses INI file adding extends functionality via ":base" postfix on namespace.
     *
     * @param string $filename
     * @return array
     */
   
function parse_ini_file_extended($filename) {
       
$p_ini parse_ini_file($filenametrue);
       
$config = array();
        foreach(
$p_ini as $namespace => $properties){
            list(
$name$extends) = explode(':'$namespace);
           
$name trim($name);
           
$extends trim($extends);
           
// create namespace if necessary
           
if(!isset($config[$name])) $config[$name] = array();
           
// inherit base namespace
           
if(isset($p_ini[$extends])){
                foreach(
$p_ini[$extends] as $prop => $val)
                   
$config[$name][$prop] = $val;
            }
           
// overwrite / set current namespace values
           
foreach($properties as $prop => $val)
           
$config[$name][$prop] = $val;
        }
        return 
$config;
    }
?>

Treats this ini:
<?php 
/*
[base]
host=localhost
user=testuser
pass=testpass
database=default

[users:base]
database=users

[archive : base]
database=archive
*/
?>
As if it were like this:
<?php
/*
[base]
host=localhost
user=testuser
pass=testpass
database=default

[users:base]
host=localhost
user=testuser
pass=testpass
database=users

[archive : base]
host=localhost
user=testuser
pass=testpass
database=archive
*/
?>
2009-04-30 19:01:16
http://php5.kiev.ua/manual/ru/function.parse-ini-file.html
.ini files or JSON file format as it is also known as, are very useful format to store stuff in. Especially large arrays. 

Strangely enough there is this nice function to read the file, but no function to write it.

So here is one.

Use it as:  put_ini_file(string $file, array $array)

<?php 
function put_ini_file($file$array$i 0){
 
$str="";
  foreach (
$array as $k => $v){
    if (
is_array($v)){
     
$str.=str_repeat(" ",$i*2)."[$k]".PHP_EOL
     
$str.=put_ini_file("",$v$i+1);
    }else
     
$str.=str_repeat(" ",$i*2)."$k = $v".PHP_EOL
  }
 if(
$file)
    return 
file_put_contents($file,$str);
  else
    return 
$str;
}
?>
2013-02-21 21:23:36
http://php5.kiev.ua/manual/ru/function.parse-ini-file.html
Автор:
You may want, in some very special cases, to parse multi-dimensional array with N levels in your ini file. Something like setting[data][config][debug] = true will result in an error (expected "=").

Here's a little function to match this, using dots (customizable).
<?php
function parse_ini_file_multi($file$process_sections false$scanner_mode INI_SCANNER_NORMAL) {
   
$explode_str '.';
   
$escape_char "'";
   
// load ini file the normal way
   
$data parse_ini_file($file$process_sections$scanner_mode);
    if (!
$process_sections) {
       
$data = array($data);
    }
    foreach (
$data as $section_key => $section) {
       
// loop inside the section
       
foreach ($section as $key => $value) {
            if (
strpos($key$explode_str)) {
                if (
substr($key01) !== $escape_char) {
                   
// key has a dot. Explode on it, then parse each subkeys
                    // and set value at the right place thanks to references
                   
$sub_keys explode($explode_str$key);
                   
$subs =& $data[$section_key];
                    foreach (
$sub_keys as $sub_key) {
                        if (!isset(
$subs[$sub_key])) {
                           
$subs[$sub_key] = [];
                        }
                       
$subs =& $subs[$sub_key];
                    }
                   
// set the value at the right place
                   
$subs $value;
                   
// unset the dotted key, we don't need it anymore
                   
unset($data[$section_key][$key]);
                }
               
// we have escaped the key, so we keep dots as they are
               
else {
                   
$new_key trim($key$escape_char);
                   
$data[$section_key][$new_key] = $value;
                    unset(
$data[$section_key][$key]);
                }
            }
        }
    }
    if (!
$process_sections) {
       
$data $data[0];
    }
    return 
$data;
}
?>

The following file:
<?php
/*
[normal]
foo = bar
; use quotes to keep your key as it is
'foo.with.dots' = true

[array]
foo[] = 1
foo[] = 2

[dictionary]
foo[debug] = false
foo[path] = /some/path

[multi]
foo.data.config.debug = true
foo.data.password = 123456
*/
?>

will result in:
<?php
parse_ini_file_multi
('file.ini'true);

Array
(
    [
normal] => Array
        (
            [
foo] => bar
           
[foo.with.dots] => 1
       
)
    [array] => Array
        (
            [
foo] => Array
                (
                    [
0] => 1
                   
[1] => 2
               
)
        )
    [
dictionary] => Array
        (
            [
foo] => Array
                (
                    [
debug] => 
                    [
path] => /some/path
               
)
        )
    [
multi] => Array
        (
            [
foo] => Array
                (
                    [
data] => Array
                        (
                            [
config] => Array
                                (
                                    [
debug] => 1
                               
)
                            [
password] => 123456
                       
)
                )
        )
)
?>
2014-04-13 12:59:39
http://php5.kiev.ua/manual/ru/function.parse-ini-file.html
This core function won't handle ini key[][] = value(s), (multidimensional arrays), so if you need to support that kind of setup you will need to write your own function. one way to do it is to convert all the key = value(s) to array string [key][][]=value(s), then use parse_str() to convert all those [key][][]=value(s) that way you just read the ini file line by line, instead of doing crazy foreach() loops to handle those (multidimensional arrays) in each section, example...

ini file...... config.php

<?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"

[fourth_section]

a[][][] = b
a
[][][][] = c
a
[test_test][][] = d
test
[one][two][three] = true

?>

echo parse_ini_file ( "C:\\services\\www\\docs\\config.php" );

results in...

// PHP Warning:  syntax error, unexpected TC_SECTION, expecting '=' line 27 -> a[][][] = b

Here it simple function that handles (multidimensional arrays) without looping each key[][]= value(s)

<?php

function getIni $file$sections FALSE )
{
   
$return = array ();

   
$keeper = array ();

   
$config fopen $file'r' );

    while ( ! 
feof $config ) )
    { 
       
$line trim fgets $config1024 ) );

       
$line = ( $line == '' ) ? ' ' $line;

        switch ( 
$line{0} )
        {
            case 
' ':
            case 
'#':
            case 
'/':
            case 
';':
            case 
'<':
            case 
'?':

            break;

            case 
'[':

            if ( 
$sections )
            {
               
$header 'config[' trim substr $line1, -) ) . ']';
            }
            else
            {
               
$header 'config';
            }

            break;

            default:

           
$kv array_map 'trim'explode '='$line ) );

           
$kv[0] = str_replace ' ''+'$kv[0] );

           
$kv[1] = str_replace ' ''+'$kv[1] );

            if ( ( 
$pos strpos $kv[0], '[' ) ) !== FALSE )
            {
               
$kv[0] = '[' substr $kv[0], 0$pos ) . ']' substr $kv[0], $pos );
            }
            else
            {
               
$kv[0] = '[' $kv[0] . ']';
            }

           
$bt strtolower $kv[1] );

            if ( 
in_array $bt, array ( 'true''false''on''off' ) ) )
            {
               
$kv[1] = ( $bt == 'true' || $bt == 'on' ) ? TRUE FALSE;
            }

           
$keeper[] = $header $kv[0] . '=' $kv[1];
        }
    }

   
fclose $config );

   
parse_str implode '&'$keeper ), $return );

    return 
$return['config'];
}

// usage...

$sections TRUE;

print_r $config->getIni "C:\\services\\www\\docs\\config.php" ),  $sections );

?>
2015-10-15 09:39:32
http://php5.kiev.ua/manual/ru/function.parse-ini-file.html
The documentation states:
Characters ?{}|&~!()^" must not be used anywhere in the key and have a special meaning in the value.

Here's the results of my experiments on what they mean:

; | is used for bitwise OR
three = 2|3

; & is used for bitwise AND
four = 6&5

; ^ is used for bitwise XOR
five = 3^6

; ~ is used for bitwise negate
negative_two = ~1

; () is used for grouping
seven = (8|7)&(6|5)

; ${...} is used for grabbing values from the environment, or previously defined values.
path = ${PATH}
also = ${five}

; ? I have no guess for
; ! I have no guess for
2016-03-09 23:21:38
http://php5.kiev.ua/manual/ru/function.parse-ini-file.html
Автор:
This function for save ini files

<?php
function array_to_ini($array,$out="")
{
   
$t="";
   
$q=false;
    foreach(
$array as $c=>$d)
    {
        if(
is_array($d))$t.=array_to_ini($d,$c);
        else
        {
            if(
$c===intval($c))
            {
                if(!empty(
$out))
                {
                   
$t.="\r\n".$out." = \"".$d."\"";
                    if(
$q!=2)$q=true;
                }
                else 
$t.="\r\n".$d;
            }
            else
            {   
               
$t.="\r\n".$c." = \"".$d."\"";
               
$q=2;
            }
        }
    }
    if(
$q!=true && !empty($out)) return "[".$out."]\r\n".$t;
    if(!empty(
$out)) return  $t;
    return 
trim($t);
}

function 
save_ini_file($array,$file)
{
   
$a=array_to_ini($array);
   
$ffl=fopen($file,"w");
   
fwrite($ffl,$a);
   
fclose($ffl);
}
?>
2016-07-03 23:49:05
http://php5.kiev.ua/manual/ru/function.parse-ini-file.html
Not mentioned in the documentation, this function acts like include:

"Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing."

(At least for PHP 7; have not checked PHP 5.)
2016-12-16 04:26:42
http://php5.kiev.ua/manual/ru/function.parse-ini-file.html
fix a little bug (here) in function put_ini_file:

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 .= "\n[$i]".PHP_EOL.put_ini_file($v, $file, false, false);
            }
            else{
                if(is_array($v)){
                    foreach($v as $t=>$m){
//--->>> Here                        $fileContent .= "-->$i[$t] = ".(is_numeric($m) ? $m : '"'.$m.'"').PHP_EOL;
                        $fileContent .= "$i"."[] = ".(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;
}
2023-09-26 17:34:20
http://php5.kiev.ua/manual/ru/function.parse-ini-file.html
Автор:
Securing your .ini file:

“ini files are generally treated as plain text by web servers and thus served to browsers if requested. That means for security you must either keep your ini files outside of your docroot or reconfigure your web server to not serve them. Failure to do either of those may introduce a security risk.”

Alternatively, you can save you file as:

stuff.ini.php

add this to the beginning:

;<?php die('go away'); ?>

The semicolon at the beginning is treated as comment, so this line has no effect on the ini file.

Since the file has a .php extension, it will run through the PHP interpreter if you attempt to access this file directly, and the php block will be processed and exit.

The file extension has no ill effect on the parse_ini_file() function, and the .ini part is, of course, a matter of taste.
2023-10-30 11:49:28
http://php5.kiev.ua/manual/ru/function.parse-ini-file.html
To make the content available in every corner of you application I use a user defined constant. $SETTINGS. It is initialized like this
<?php

define
'SETTINGS", parse_ini_file('settings.ini', true) );

?>
With the proper settings.ini file you can now do stuff like 
<?php
$db = new \PDO(
  "mysql:host={SETTINGS['
db']['host']};dbname={SETTINGS['db']['name']};charset=utf8",
  SETTINGS['
db']['user'],
  SETTINGS['
db']['pass'], [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
  ]
);
?>

Make sure to hide your settings.ini file on a website with for instance
<?php
<FilesMatch "\.(?:ini|htaccess)$">
  Order allow,deny
  Deny from all
</FilesMatch>
?>
2024-02-26 10:18:58
http://php5.kiev.ua/manual/ru/function.parse-ini-file.html

    Поддержать сайт на родительском проекте КГБ