PHP include relative path

PHP include relative path

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why set_include_path Cannot Override Relative Path Resolution

The behavior you are seeing stems from a fundamental misunderstanding of how PHP resolves file paths during inclusion. It is not that your logic is flawed; it is that you are trying to use a global configuration setting (set_include_path) to solve a problem that operates at the local scope (the current working directory).

When PHP executes include "../config.php" inside /root/connect.php, it does not scan the directories listed in the include_path. Instead, it performs a specific two-step resolution process relative to the file containing the statement. First, it checks the current working directory (where the script was launched from). Second, and only if that fails, it attempts to resolve the relative path against the include_path directories.

In your scenario, you are running /root/update/test.php. When this script hits include "connect.php", PHP loads that file into memory. At that exact moment, the "current working directory" is /root/update/. Consequently, when connect.php executes include "../config.php", PHP interprets that relative path as /root/config.php.

This is where the confusion arises. You noted that /root/config.php exists. However, your error log indicates a failure to open /config.php. This discrepancy suggests one of two things: either the file actually resides at /config.php on your system (which would be an absolute path disguised as relative), or, more likely in a standard Unix environment, the root directory (/) is not where you expect your files to be, and the ../ traversal is taking you somewhere unexpected or non-existent.

The set_include_path(".:/root") call in test.php modifies the global search path for absolute relative paths (like "file.php"). It effectively tells PHP: "If I don't find this file in the current directory, look in /root." This works perfectly for finding connect.php. However, it does not retroactively alter how the string "../config.php" is parsed. The dot-slash notation explicitly demands a traversal relative to the current working directory, completely bypassing the include_path lookup entirely.

Why Relative Paths Are Fragile in Deployment

The core issue here is that relative paths are inherently tied to the execution context of the script, not the script's location. If you deploy your application to a web server where the entry point changes, or if the request comes through an Apache proxy that alters the working directory, your relative path logic breaks instantly. This is why relying on ../ for internal configuration inclusion is widely discouraged in production environments.

Instead of fighting the resolution order with set_include_path, you should align your file structure to match your intended paths. If connect.php needs config.php, they should reside in a shared directory, or connect.php should be required from an absolute path determined at runtime.

A Robust Alternative: Autoloading and Namespaces

Given that you cannot use absolute paths due to production constraints, the most resilient approach is to structure your code so that relative dependencies are minimized or handled via a standard autoloader. If you are working within a larger application framework, switching to an autoloading strategy (such as PSR-4) allows you to define class locations based on namespace hierarchies rather than directory traversal. This decouples the file location from the specific include_path or current working directory state.

If you must stick with static includes, ensure that your relative paths are consistent regardless of where the script is invoked. A common pattern is to define a base path constant at the very top of every file:

define('BASE_PATH', dirname(__DIR__)); // Adjust based on actual structure
include BASE_PATH . '/config.php';

This approach ensures that you are always referencing a specific logical directory rather than relying on the dynamic current working directory. While set_include_path is useful for legacy codebases or specific legacy library requirements, it should not be used to fix relative path resolution logic. The warning you received is PHP correctly informing you that it followed your explicit instruction to go up one level, and that destination simply does not exist in the context of your current working directory.

For modern projects requiring strict dependency management without absolute paths, frameworks like Laravel provide robust autoloading mechanisms that handle these path resolutions internally. You can explore how they handle this complexity at https://laravelcompany.com.

Our network