Current Location: Home> Latest Articles> Dynamically load multiple files with get_include_path() and glob() functions

Dynamically load multiple files with get_include_path() and glob() functions

gitbox 2025-05-29

In PHP project development, we often need to load class files, configuration files, or other resources from multiple directories. Especially in large projects, modular design prompts us to spread our code under different directory structures. In order to improve the maintainability and flexibility of the code, combining get_include_path() and glob() to dynamically load files under multiple paths is a very practical technical method.

Understand get_include_path() and glob()

get_include_path()

The get_include_path() function is used to get the current include_path setting. This path list is usually used to make include or require lookup files, and can contain multiple directories, separated by system-specific path separators (such as colons on UNIX , semicolons on Windows ; ).

For example:

 echo get_include_path();
// Output:.:/usr/local/lib/php:/home/user/myapp/includes

glob()

The glob() function is used to find matching file paths based on patterns. It supports wildcard characters, and the most commonly used is * to represent any file name.

Example:

 $files = glob('/path/to/dir/*.php');

The above code will look for all .php files in the directory /path/to/dir/ .

Implement dynamic loading of files under multiple paths

In order to automatically load matching files from multiple include_paths, we can combine get_include_path() and glob() to do it. Here is the complete sample code:

 function loadFilesFromIncludePaths($pattern) {
    $includePaths = explode(PATH_SEPARATOR, get_include_path());
    
    foreach ($includePaths as $path) {
        $fullPattern = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $pattern;
        $files = glob($fullPattern);
        
        if (!empty($files)) {
            foreach ($files as $file) {
                if (is_file($file)) {
                    require_once $file;
                }
            }
        }
    }
}

Example of usage

Suppose your include_path is set as follows:

 set_include_path(
    '/var/www/gitbox.net/modules:' .
    '/var/www/gitbox.net/plugins:' .
    get_include_path()
);

And you want to load the init.php file in all directories, just call:

 loadFilesFromIncludePaths('init.php');

This will automatically find and load all files named init.php under /var/www/gitbox.net/modules and /var/www/gitbox.net/plugins .

Advantages Analysis

  • Modular loading : Each module comes with initialization logic, named init.php , and the system is loaded uniformly.

  • Low coupling : The new module does not need to modify the main loading logic, it can be automatically recognized by simply placing the file.

  • Flexible configuration : By setting include_path, you can easily adjust or add new module directories.

Things to note

  1. Security : Avoid including unverified user upload paths.

  2. Performance considerations : glob() is a file system-based operation and is not suitable for frequent call-to-use high-concurrency scenarios.

  3. Path delimiter compatibility : Ensure the use of PATH_SEPARATOR and DIRECTORY_SEPARATOR constants in a cross-platform environment.

Summarize

Using get_include_path() with glob() can greatly improve the flexibility and maintainability of PHP programs in modular design. This method is particularly suitable for scenarios where files of the same type need to be loaded dynamically in multiple paths, such as plug-in systems, module initialization, etc. By reasonably setting include_path and file naming specifications, an efficient and automated loading mechanism can be built to provide powerful expansion capabilities for large PHP projects.