<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.filesystem.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.parse-ini-file.php',
    1 => 'parse_ini_file',
    2 => 'Parse a configuration file',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Filesystem Functions',
  ),
  'prev' => 
  array (
    0 => 'function.move-uploaded-file.php',
    1 => 'move_uploaded_file',
  ),
  'next' => 
  array (
    0 => 'function.parse-ini-string.php',
    1 => 'parse_ini_string',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/filesystem/functions/parse-ini-file.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.parse-ini-file" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">parse_ini_file</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">parse_ini_file</span> &mdash; <span class="dc-title">Parse a configuration file</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.parse-ini-file-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>parse_ini_file</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$filename</code></span>, <span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$process_sections</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$scanner_mode</code><span class="initializer"> = <strong><code><a href="filesystem.constants.php#constant.ini-scanner-normal">INI_SCANNER_NORMAL</a></code></strong></span></span>): <span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>parse_ini_file()</strong></span> loads in the
   ini file specified in <code class="parameter">filename</code>,
   and returns the settings in it in an associative array.
  </p>
  <p class="para">
   The structure of the ini file is the same as the <var class="filename">php.ini</var>&#039;s.
  </p>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="simpara">
    This function must not be used with untrusted inputs, unless
    <code class="parameter">scanner_mode</code> is <strong><code><a href="filesystem.constants.php#constant.ini-scanner-raw">INI_SCANNER_RAW</a></code></strong>
    since the parsed output might contain the values of sensitive
    constants, such as constants holding a database password.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.parse-ini-file-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">filename</code></dt>
     <dd>
      <p class="para">
       The filename of the ini file being parsed. If a relative path is used,
       it is evaluated relative to the current working directory, then the
       <a href="ini.core.php#ini.include-path" class="link">include_path</a>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">process_sections</code></dt>
     <dd>
      <p class="para">
       By setting the <code class="parameter">process_sections</code>
       parameter to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, you get a multidimensional array, with
       the section names and settings included. The default
       for <code class="parameter">process_sections</code> is <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>      
      </p>
     </dd>
    
    
     <dt><code class="parameter">scanner_mode</code></dt>
     <dd>
      <p class="para">
       Can either be <strong><code><a href="filesystem.constants.php#constant.ini-scanner-normal">INI_SCANNER_NORMAL</a></code></strong> (default) or 
       <strong><code><a href="filesystem.constants.php#constant.ini-scanner-raw">INI_SCANNER_RAW</a></code></strong>. If <strong><code><a href="filesystem.constants.php#constant.ini-scanner-raw">INI_SCANNER_RAW</a></code></strong> 
       is supplied, then option values will not be parsed.
      </p>
      <p class="para">
    As of PHP 5.6.1 can also be specified as <strong><code><a href="filesystem.constants.php#constant.ini-scanner-typed">INI_SCANNER_TYPED</a></code></strong>.
    In this mode boolean, null and integer types are preserved when possible.
    String values <code class="literal">&quot;true&quot;</code>, <code class="literal">&quot;on&quot;</code> and <code class="literal">&quot;yes&quot;</code>
    are converted to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>. <code class="literal">&quot;false&quot;</code>, <code class="literal">&quot;off&quot;</code>, <code class="literal">&quot;no&quot;</code>
    and <code class="literal">&quot;none&quot;</code> are considered <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>. <code class="literal">&quot;null&quot;</code> is converted to <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>
    in typed mode. Also, all numeric strings are converted to integer type if it is possible.
   </p>
     </dd>
    
   </dl>
  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.parse-ini-file-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   The settings are returned as an associative <span class="type"><a href="language.types.array.php" class="type array">array</a></span> on success,
   and <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.parse-ini-file-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 Contents of <var class="filename">sample.ini</var></strong></p>
    <div class="example-contents">
<div class="cdata"><pre>
; This is a sample configuration file
; Comments start with &#039;;&#039;, as in php.ini

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

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

[third_section]
phpversion[] = &quot;5.0&quot;
phpversion[] = &quot;5.1&quot;
phpversion[] = &quot;5.2&quot;
phpversion[] = &quot;5.3&quot;

urls[svn] = &quot;http://svn.php.net&quot;
urls[git] = &quot;http://git.php.net&quot;
</pre></div>
    </div>

   </div>
   <div class="example" id="example-2">
    <p><strong>Example #2 <span class="function"><strong>parse_ini_file()</strong></span> example</strong></p>
    <div class="example-contents"><p>
     <a href="language.constants.php" class="link">Constants</a> (but not &quot;magic constants&quot; like <strong><code><a href="language.constants.magic.php#constant.file">__FILE__</a></code></strong>)
     may also be parsed
     in the ini file so if you define a constant as an ini value before
     running <span class="function"><strong>parse_ini_file()</strong></span>, it will be integrated into
     the results.  Only ini values are evaluated, and the value must be just the constant.  For example:
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />define</span><span style="color: #007700">(</span><span style="color: #DD0000">'BIRD'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Dodo bird'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Parse without sections<br /></span><span style="color: #0000BB">$ini_array </span><span style="color: #007700">= </span><span style="color: #0000BB">parse_ini_file</span><span style="color: #007700">(</span><span style="color: #DD0000">"sample.ini"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$ini_array</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Parse with sections<br /></span><span style="color: #0000BB">$ini_array </span><span style="color: #007700">= </span><span style="color: #0000BB">parse_ini_file</span><span style="color: #007700">(</span><span style="color: #DD0000">"sample.ini"</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$ini_array</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Array
(
    [one] =&gt; 1
    [five] =&gt; 5
    [animal] =&gt; Dodo bird
    [path] =&gt; /usr/local/bin
    [URL] =&gt; http://www.example.com/~username
    [phpversion] =&gt; Array
        (
            [0] =&gt; 5.0
            [1] =&gt; 5.1
            [2] =&gt; 5.2
            [3] =&gt; 5.3
        )

    [urls] =&gt; Array
        (
            [svn] =&gt; http://svn.php.net
            [git] =&gt; http://git.php.net
        )

)
Array
(
    [first_section] =&gt; Array
        (
            [one] =&gt; 1
            [five] =&gt; 5
            [animal] =&gt; Dodo bird
        )

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

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

            [urls] =&gt; Array
                (
                    [svn] =&gt; http://svn.php.net
                    [git] =&gt; http://git.php.net
                )

        )

)</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Example #3 <span class="function"><strong>parse_ini_file()</strong></span> parsing a php.ini file</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// A simple function used for comparing the results below<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">yesno</span><span style="color: #007700">(</span><span style="color: #0000BB">$expression</span><span style="color: #007700">)<br />{<br />    return(</span><span style="color: #0000BB">$expression </span><span style="color: #007700">? </span><span style="color: #DD0000">'Yes' </span><span style="color: #007700">: </span><span style="color: #DD0000">'No'</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">// Get the path to php.ini using the php_ini_loaded_file() function<br /></span><span style="color: #0000BB">$ini_path </span><span style="color: #007700">= </span><span style="color: #0000BB">php_ini_loaded_file</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Parse php.ini<br /></span><span style="color: #0000BB">$ini </span><span style="color: #007700">= </span><span style="color: #0000BB">parse_ini_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$ini_path</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Print and compare the values, note that using get_cfg_var()<br />// will give the same results for parsed and loaded here<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'(parsed) magic_quotes_gpc = ' </span><span style="color: #007700">. </span><span style="color: #0000BB">yesno</span><span style="color: #007700">(</span><span style="color: #0000BB">$ini</span><span style="color: #007700">[</span><span style="color: #DD0000">'magic_quotes_gpc'</span><span style="color: #007700">]) . </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">'(loaded) magic_quotes_gpc = ' </span><span style="color: #007700">. </span><span style="color: #0000BB">yesno</span><span style="color: #007700">(</span><span style="color: #0000BB">get_cfg_var</span><span style="color: #007700">(</span><span style="color: #DD0000">'magic_quotes_gpc'</span><span style="color: #007700">)) . </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">(parsed) magic_quotes_gpc = Yes
(loaded) magic_quotes_gpc = Yes</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4">
    <p><strong>Example #4 Value Interpolation</strong></p>
    <div class="example-contents"><p>
     In addition to evaluating constants, certain characters have special meaning in an ini value.
     Additionally, environment variables and previously defined configuration options (see <span class="function"><a href="function.get-cfg-var.php" class="function">get_cfg_var()</a></span>) may be read using
     <code class="code">${}</code> syntax.
    </p></div>
    <div class="example-contents">
<div class="cdata"><pre>
; | is used for bitwise OR
three = 2|3

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

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

; ~ is used for bitwise negate
negative_two = ~1

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

; Interpolate the PATH environment variable
path = ${PATH}

; Interpolate the configuration option &#039;memory_limit&#039;
configured_memory_limit = ${memory_limit}

</pre></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5">
    <p><strong>Example #5 Escaping Characters</strong></p>
    <div class="example-contents"><p>
     Some characters have special meaning in double-quoted strings and must be escaped by the backslash prefix.
     First of all, these are the double quote <code class="code">&quot;</code> as the boundary marker, and the backslash <code class="code">\</code> itself
     (if followed by one of the special characters):
    </p></div>
    <div class="example-contents">
<div class="cdata"><pre>
quoted = &quot;She said \&quot;Exactly my point\&quot;.&quot; ; Results in a string with quote marks in it.
hint = &quot;Use \\\&quot; to escape double quote&quot; ; Results in: Use \&quot; to escape double quote
</pre></div>
    </div>

    <div class="example-contents"><p>
     There is an exception made for Windows-like paths: it&#039;s possible to not escape trailing backslash
     if the quoted string is directly followed by a linebreak:
    </p></div>
    <div class="example-contents">
<div class="cdata"><pre>
save_path = &quot;C:\Temp\&quot;
</pre></div>
    </div>

    <div class="example-contents"><p>
     If one does need to escape double quote followed by linebreak in a multiline value,
     it&#039;s possible to use value concatenation in the following way
     (there is one double-quoted string directly followed by another one):
    </p></div>
    <div class="example-contents">
<div class="cdata"><pre>
long_text = &quot;Lorem \&quot;ipsum\&quot;&quot;&quot;
 dolor&quot; ; Results in: Lorem &quot;ipsum&quot;\n dolor
</pre></div>
    </div>

    <div class="example-contents"><p>
     Another character with special meaning is <code class="code">$</code> (the dollar sign).
     It must be escaped if followed by the open curly brace:
    </p></div>
    <div class="example-contents">
<div class="cdata"><pre>
code = &quot;\${test}&quot;
</pre></div>
    </div>

    <div class="example-contents"><p>
     Escaping characters is not supported in the <strong><code><a href="filesystem.constants.php#constant.ini-scanner-raw">INI_SCANNER_RAW</a></code></strong> mode
     (in this mode all characters are processed &quot;as is&quot;).
    </p></div>
    <div class="example-contents"><p>
     Note that the ini parser doesn&#039;t support standard escape sequences (<code class="code">\n</code>, <code class="code">\t</code>, etc.).
     If necessary, post-process the result of <span class="function"><strong>parse_ini_file()</strong></span> with <span class="function"><a href="function.stripcslashes.php" class="function">stripcslashes()</a></span> function.
    </p></div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.parse-ini-file-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    This function has nothing to do with the
    <var class="filename">php.ini</var> file. It is already processed by
    the time you run your script. This function can be used to
    read in your own application&#039;s configuration files.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    If a value in the ini file contains any non-alphanumeric
    characters it needs to be enclosed in double-quotes (&quot;).
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    There are reserved words which must not be used as keys for
    ini files.  These include: <code class="literal">null</code>, <code class="literal">yes</code>,
    <code class="literal">no</code>, <code class="literal">true</code>, <code class="literal">false</code>,
    <code class="literal">on</code>, <code class="literal">off</code>, <code class="literal">none</code>.
    Values <code class="literal">null</code>, <code class="literal">off</code>, <code class="literal">no</code> and
    <code class="literal">false</code> result in <code class="literal">&quot;&quot;</code>, and values
    <code class="literal">on</code>, <code class="literal">yes</code> and <code class="literal">true</code> result
    in <code class="literal">&quot;1&quot;</code>, unless <strong><code><a href="filesystem.constants.php#constant.ini-scanner-typed">INI_SCANNER_TYPED</a></code></strong> mode is used.
    Characters <code class="literal">?{}|&amp;~!()^&quot;</code> must not be used anywhere in
    the key and have a special meaning in the value.
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Entries without an equal sign are ignored. For example, &quot;foo&quot;
    is ignored whereas &quot;bar =&quot; is parsed and added with an empty
    value. For example, MySQL has a &quot;no-auto-rehash&quot; setting 
    in <var class="filename">my.cnf</var> that does not take a value, so
    it is ignored.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    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.
   </p>
  </p></blockquote>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.parse-ini-file-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.parse-ini-string.php" class="function" rel="rdfs-seeAlso">parse_ini_string()</a> - Parse a configuration string</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>