Current Location: Home> Latest Articles> How to modify the included path in PHP: get_include_path() and set_include_path()

How to modify the included path in PHP: get_include_path() and set_include_path()

gitbox 2025-05-29

When developing PHP applications, it is often necessary to organize and reuse code modules between multiple directories. In order to enable statements such as include , require and other statements to correctly find these modules, PHP provides get_include_path() and set_include_path() functions to flexibly manage included paths. This article will introduce in detail the usage of these two functions and practical application scenarios.

1. get_include_path(): get the currently included path

get_include_path() is used to get the include path settings for the current PHP. The inclusion path is a list of directories where PHP looks for the included files.

Example

 echo get_include_path();

The output is similar to the following:

 .:/usr/local/lib/php

This means that PHP will first look for the file in the current directory ( . ) and then look for it in /usr/local/lib/php .

2. set_include_path(): Set a new include path

Use set_include_path() to modify the include path of PHP. This function accepts a string representing a new path list, and the directories are connected by system path separators (colons on UNIX/Linux systems : , semicolons on Windows ; ).

Example: Add a custom directory to include path

 $customPath = '/var/www/html/includes';
set_include_path(get_include_path() . PATH_SEPARATOR . $customPath);

This code adds the /var/www/html/includes directory based on the existing included path.

3. Practical application: modular development

Suppose we have a public function file functions.php , which is placed in the /var/www/html/libs directory. In order to include this file in the main program, we can do this:

 set_include_path(get_include_path() . PATH_SEPARATOR . '/var/www/html/libs');

include 'functions.php';

By setting the include path, we do not have to use relative or absolute path reference modules in each file, improving the readability and maintainability of the code.

4. Use stream_resolve_include_path() to confirm whether the file can be included

To confirm whether a file can be found through the current include path, you can use the stream_resolve_include_path() function:

 $file = 'config.php';
$resolvedPath = stream_resolve_include_path($file);

if ($resolvedPath !== false) {
    echo "File location: $resolvedPath";
} else {
    echo "File cannot be found $file";
}

5. Use in conjunction with URLs (for example, including remote files through fopen)

It should be noted that although include and require can support URL wrappers (such as http:// ), this method is usually not recommended in actual production environments. If you want to use it, you can do this:

 $url = 'http://gitbox.net/includes/config.php';
include $url;

In order for this remote inclusion to work properly, the allow_url_include setting must be turned on (default is turned off for security reasons).

It can be modified in php.ini:

 allow_url_include = On

Or dynamically set it through code:

 ini_set('allow_url_include', '1');

6. Summary

  • Use get_include_path() to view the current include path settings.

  • Use set_include_path() to dynamically add or modify paths, making it convenient for code modularity.

  • Together with stream_resolve_include_path(), you can verify whether the file can be found.

  • Although remote inclusion is supported, it should be used with caution and local path management should be preferred.

By using these functions reasonably, the structural clarity and maintainability of PHP projects can be greatly improved.