PHP include absolute path

PHP include absolute path

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

Stefan Bogdanescu

Founder & Senior Architect

PHP include absolute path

A common stumbling block in legacy PHP projects involves mixing relative and absolute file paths within a single application structure. You might define a base path variable, such as $basePath = '/Systems/dgw/';, and successfully load static assets like CSS, JavaScript, and images using this variable combined with relative sub-paths. When you apply the same logic to an include statement expecting it to behave identically to your asset links, it often fails unexpectedly.

The core issue here is a fundamental misunderstanding of how PHP resolves paths for different file types. While HTML tags like <link> and <img> resolve their href and src attributes relative to the current document's location in the web server's root, PHP functions like include, require, or require_once resolve their arguments based on the operating system's file system rules, not the HTTP request context.

In your specific scenario, the line <img src="<?php echo $basePath; ?>images/new_logo.png" alt="logo"/> works because the web server (Apache, Nginx, etc.) serves this file as a static resource. The browser requests the image using the URL constructed by that string. However, when you execute <?php include($basePath.'include/assets/common/topMessages.php'); ?>, you are instructing the PHP interpreter to locate a physical file on the disk.

The variable $basePath is set to /Systems/dgw/. This looks like an absolute path starting from the root of the filesystem (Linux, macOS) or potentially a virtual host root (Windows), but the behavior depends entirely on your server configuration. If this is running on a standard Linux Apache setup where the web root is /var/www/html, the path /Systems/dgw/ refers to a location outside the typical document tree unless specifically aliased or mounted there.

The fact that the relative path version works (../../include/assets/common/topMessages.php) reveals the true physical location of your include file. It confirms that topMessages.php resides in a directory reachable via parent directory traversal from your current script's execution context. This implies that $basePath does not correctly point to the directory containing include/assets/common/.

If $basePath represents the web root (e.g., /var/www/html), then concatenating it with /include/assets/common/topMessages.php would result in a filesystem path like /var/www/html/include/assets/common/topMessages.php. If your actual file structure places that include folder inside ../../, then the variable is pointing to the wrong starting point, or it is pointing to a virtual directory that does not map 1:1 to the physical disk layout.

Consider the distinction between URL paths and filesystem paths. Static assets use URLs. When you write <link href="/Systems/dgw/include/assets/css/bootstrap.min.css">, the web server translates this URL into a filesystem path to serve the file. PHP includes, however, bypass the web server's URL translation layer for the specific file lookup. They look strictly at the filesystem.

If $basePath is intended to be an absolute filesystem path, it must match the exact location of the include directory on your disk. If your project structure is:
/var/www/html/ (Web Root)
└── include/assets/common/topMessages.php

And your code uses $basePath = '/Systems/dgw/', but the physical file is located at /var/www/html/include/..., then the concatenation fails. The web server might handle /Systems/dgw/ via a .htaccess redirect or alias, making static assets loadable, but PHP's include will not follow that alias unless the filesystem path itself matches.

To resolve this without hardcoding relative paths that break when moving files, you can use PHP's built-in constants to derive the correct directory dynamically. Instead of relying on a custom variable that might be ambiguous between URL and file system contexts, use dirname(__DIR__) combined with __FILE__ logic if you need absolute resolution from the current script.

Alternatively, if the goal is to ensure portability across environments (development vs. production), consider defining a constant at the top of your project that resolves the base directory relative to the current file:

define('BASE_DIR', dirname(__DIR__)); 
// Or if you need it relative to the script itself
define('BASE_PATH', dirname(__FILE__));

Then, construct your include path by appending the relative subdirectory to this resolved base. This ensures that whether $basePath was meant to be a URL or a file path, you are explicitly constructing the filesystem path required for include.

There is a specific edge case where the variable works for URLs but fails for includes: virtual hosting on Windows IIS or Apache where /Systems/dgw/ is a mapped drive letter (e.g., Z:) or a symlink. PHP's include follows symlinks, but if the mapping is only active for URL rewriting and not exposed as a raw filesystem path to the PHP interpreter in that context, the include will throw an "failed to open stream" error.

Ultimately, the solution lies in ensuring that the string passed to include() points to a valid location on the disk, regardless of how browsers interpret the same string for asset loading. If your application relies heavily on absolute paths defined by variables, always verify that those variables represent physical filesystem locations when used with include, rather than URL prefixes.

Our network