Current Location: Home> Latest Articles> Use get_include_path() to read the configuration file path with parse_ini_file()

Use get_include_path() to read the configuration file path with parse_ini_file()

gitbox 2025-05-26

Reading configuration files is a very common requirement in PHP project development. Configuration files are usually in .ini format and are read through the parse_ini_file() function. However, when the project structure is complex and the configuration files are distributed across multiple directories, hard-coded paths reduce the maintainability and portability of the code.

To solve this problem, this article introduces how to use get_include_path() and parse_ini_file() to flexibly locate and read configuration files to improve the scalability and robustness of the code.

Review of basic knowledge

parse_ini_file()

parse_ini_file() is a function provided by PHP to parse files in .ini format into arrays. The usage is as follows:

 $config = parse_ini_file('/path/to/config.ini');

It supports the second parameter setting whether to parse the section (Section):

 $config = parse_ini_file('/path/to/config.ini', true);

get_include_path()

get_include_path() returns the current include_path, which is the list of paths that PHP searches for files when using functions such as include or require . Multiple paths are separated by PATH_SEPARATOR (on UNIX : , on Windows ; ).

For example:

 echo get_include_path();
// Output:/var/www/config:/usr/share/php

Flexible search of configuration file paths

Adding the configuration file path to include_path, we can use this mechanism to traverse all paths to find the target configuration file without hard-coded the full path.

The following is a practical function example that automatically searches and parses configuration files in include_path:

 function load_config($filename, $use_sections = false) {
    $paths = explode(PATH_SEPARATOR, get_include_path());
    foreach ($paths as $path) {
        $full_path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $filename;
        if (file_exists($full_path)) {
            return parse_ini_file($full_path, $use_sections);
        }
    }
    throw new Exception("Configuration File '{$filename}' not found。");
}

Example of usage

Suppose your project structure is as follows:

 /project
├── config/
│   └── app.ini
├── public/
│   └── index.php

You can set it like this in the project entry file index.php :

 set_include_path(get_include_path() . PATH_SEPARATOR . '/project/config');

try {
    $config = load_config('app.ini', true);
    echo 'Database Host: ' . $config['database']['host'];
} catch (Exception $e) {
    echo 'mistake: ' . $e->getMessage();
}

Application scenario expansion

  1. Multi-environment configuration switching <br> Set multiple include_paths, such as /project/config/dev and /project/config/prod , add different paths according to the current environment, and automatically load the corresponding configuration.

  2. Modular configuration <br> Each module has a configuration file, and the module path is added to include_path to implement unified loading logic.

  3. Plug-in system support <br> The plug-in can register its own configuration file path to include_path without explicitly specifying the main program.

Safety advice

  • Do not place .ini files containing sensitive information in a path that is accessible to the web, such as https://gitbox.net/config/app.ini .

  • Use .htaccess or web server configuration to prohibit access to the configuration file directory.

Summarize

By combining get_include_path() with parse_ini_file() , configuration file path management issues can be elegantly solved. It not only reduces path hard coding, but also improves the flexibility and modularity of the system. Mastering this technique is particularly important for the architecture of medium and large PHP projects.