Php using ini file

How to read and write INI files in PHP?

INI files are simple text files with a basic structure consisting of sections, property and value. The INI files can be used across different platform which is a great advantage as a single INI file can be used in various versions of the same application for various platforms.

INI files are used to store configurations for applications in a manner of property = value. Values can be single or one dimensional array. Also sections can be used to group same type of configurations. The last thing is comments, we can write comments in INI files. Comments in INI files starts with ; (semicolon)

Sample INI File

Following is a sample INI files which has comments, sections, property and values.

; this is our configuration file for the app that doesn't even exist :P [App Config] name = "Unborn Application" version = "1.0.C" [Authentication] allow registration = 1 allow facebook_registration = 1 allow gmail_registration = 0 [Commenting] allow commenting = 1 allow anonymous = "some data here" [paths] paths[root] = '/' paths[uploads] = "/contents/uploads"

Syntax for INI Files

  1. Use semicolon (;) for comments
  2. Section names must be inside square brackets []
  3. Each property should start at new line followed by an equal which is followed by value of the property. Such as, property = value
  4. String values of properties should be inside single or double quotes
  5. Numeric values should not be inside quotes
  6. Arrays can be indexed; such as paths[root] or non-indexed such as paths[]. If non-indexed, numeric indexes will be placed in result as usual
  7. Only supports one dimensional arrays

Reading an INI File in PHP

Okay so to read this INI file in PHP, just make a call to the function parse_ini_file() as follows

Here, the first parameter is the path to the INI files and the second parameter indicates to include sections in the output which is by default false. However the output of the $config array would like following

Array ( [App Config] => Array ( [name] => "Unborn Application" [version] => "1.0.C" ) [Authentication] => Array ( [allow registration] => 1 [allow facebook_registration] => 1 [allow gmail_registration] => 0 ) [Commenting] => Array ( [allow commenting] => 1 [allow anonymous] => "some data here" ) [paths] => Array ( [paths] => Array ( [root] => "/" [uploads] => "/contents/uploads" ) ) )

If we do not want to use sections in output, then we should not send the second parameter. Anyway, reading is simple but sadly there is no direct function from PHP to modify an existing configuration in an existing INI file or creating a whole new INI file. But no worry, along with the following one there are so many custom functions to write or update configurations in INI file from PHP

Читайте также:  Тег TD, атрибут align

Editing or Updating an INI file in PHP

The following function consider that you have already parsed configuration from the INI file and now you have the configuration in an array. You modify the configuration array as you want then you send the array to the function to update or rewrite the INI file with the populated configuration array.

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 .= "[$i]".PHP_EOL.put_ini_file($v, $file, false, false); >else< if(is_array($v))< foreach($v as $t=>$m) < $fileContent .= "$i[$t] = ".(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; >

So, you call the above function like put_ini_file($configArray, “path/to/config.ini”, true);

However, you don’t send the 3rd parameter as true if you have no section in your configuration in INI, just send the first two parameters.

Источник

Php using ini file

The configuration file ( php.ini ) is read when PHP starts up. For the server module versions of PHP, this happens only once when the web server is started. For the CGI and CLI versions, it happens on every invocation.

  • SAPI module specific location ( PHPIniDir directive in Apache 2, -c command line option in CGI and CLI)
  • The PHPRC environment variable.
  • The location of the php.ini file can be set for different versions of PHP. The root of the registry keys depends on 32- or 64-bitness of the installed OS and PHP. For 32-bit PHP on a 32-bit OS or a 64-bit PHP on a 64-bit OS use [(HKEY_LOCAL_MACHINE\SOFTWARE\PHP] for 32-bit version of PHP on a 64-bit OS use [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\PHP] ] instead. For same bitness installation the following registry keys are examined in order: [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y.z] , [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y] and [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x] , where x, y and z mean the PHP major, minor and release versions. For 32 bit versions of PHP on a 64 bit OS the following registry keys are examined in order: [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x.y.z] , [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x.y] and [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x] , where x, y and z mean the PHP major, minor and release versions. If there is a value for IniFilePath in any of these keys, the first one found will be used as the location of the php.ini (Windows only).
  • [HKEY_LOCAL_MACHINE\SOFTWARE\PHP] or [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\PHP] , value of IniFilePath (Windows only).
  • Current working directory (except CLI).
  • The web server’s directory (for SAPI modules), or directory of PHP (otherwise in Windows).
  • Windows directory ( C:\windows or C:\winnt ) (for Windows), or —with-config-file-path compile time option.
Читайте также:  Index php ukey auxpage

If php-SAPI.ini exists (where SAPI is the SAPI in use, so, for example, php-cli.ini or php-apache.ini ), it is used instead of php.ini . The SAPI name can be determined with php_sapi_name() .

Note:

The Apache web server changes the directory to root at startup, causing PHP to attempt to read php.ini from the root filesystem if it exists.

Using environment variables can be used in php.ini as shown below.

Example #1 php.ini Environment Variables

; PHP_MEMORY_LIMIT is taken from environment memory_limit = $

The php.ini directives handled by extensions are documented on the respective pages of the extensions themselves. A list of the core directives is available in the appendix. Not all PHP directives are necessarily documented in this manual: for a complete list of directives available in your PHP version, please read your well commented php.ini file. Alternatively, you may find » the latest php.ini from Git helpful too.

Example #2 php.ini example

; any text on a line after an unquoted semicolon (;) is ignored [php] ; section markers (text within square brackets) are also ignored ; Boolean values can be set to either: ; true, on, yes ; or false, off, no, none register_globals = off track_errors = yes ; you can enclose strings in double-quotes include_path = ".:/usr/local/lib/php" ; backslashes are treated the same as any other character include_path = ".;c:\php\lib"

It is possible to refer to existing .ini variables from within .ini files. Example: open_basedir = $ «:/new/dir» .

Scan directories

It is possible to configure PHP to scan for .ini files in a directory after reading php.ini . This can be done at compile time by setting the —with-config-file-scan-dir option. The scan directory can then be overridden at run time by setting the PHP_INI_SCAN_DIR environment variable.

Читайте также:  Sql server 2012 with php

It is possible to scan multiple directories by separating them with the platform-specific path separator ( ; on Windows, NetWare and RISC OS; : on all other platforms; the value PHP is using is available as the PATH_SEPARATOR constant). If a blank directory is given in PHP_INI_SCAN_DIR , PHP will also scan the directory given at compile time via —with-config-file-scan-dir.

Within each directory, PHP will scan all files ending in .ini in alphabetical order. A list of the files that were loaded, and in what order, is available by calling php_ini_scanned_files() , or by running PHP with the —ini option.

Assuming PHP is configured with --with-config-file-scan-dir=/etc/php.d, and that the path separator is . $ php PHP will load all files in /etc/php.d/*.ini as configuration files. $ PHP_INI_SCAN_DIR=/usr/local/etc/php.d php PHP will load all files in /usr/local/etc/php.d/*.ini as configuration files. $ PHP_INI_SCAN_DIR=:/usr/local/etc/php.d php PHP will load all files in /etc/php.d/*.ini, then /usr/local/etc/php.d/*.ini as configuration files. $ PHP_INI_SCAN_DIR=/usr/local/etc/php.d: php PHP will load all files in /usr/local/etc/php.d/*.ini, then /etc/php.d/*.ini as configuration files.

Источник

Оцените статью