How to include PHP files that require an absolute path?

How to include PHP files that require an absolute path?

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

Stefan Bogdanescu

Founder & Senior Architect

How to include PHP files that require an absolute path?

You are facing a common structural dilemma: your application logic relies on a specific directory hierarchy, but the entry points for your project live in different locations. When you execute script.php, the relative path to your includes resolves correctly from the current working directory. However, running /soap/soap.php changes the context; now that same relative path points to a completely different filesystem location, or fails entirely if it requires traversing up directories with ../. While hardcoding an absolute path like /mnt/webdev/[project name]/inc/include1.php ensures functionality, it couples your code tightly to the server's root and breaks immediately if you deploy to a new environment.

The standard solution is not to guess the path every time, but to define a canonical "base" for your project at runtime. This approach decouples your file logic from the server's absolute filesystem location. Instead of scattering include statements with varying relative directions or hardcoding full paths, you establish a single reference point that works regardless of which script initiates the execution.

The most robust pattern in PHP is to define a constant early in your application lifecycle that represents the root directory of your project. You can determine this by inspecting the __FILE__ variable or the entry point itself. For instance, if script.php is located at /mnt/webdev/myproject/script.php, you can derive the base path by stripping the filename from the full path:

<?php

// Determine the script's directory regardless of how it was called
$baseDir = dirname(__DIR__); // Assuming project root is one level up or same level depending on structure

// Define a constant for consistent use throughout the application
define('PROJECT_ROOT', $baseDir);

// Now your includes are relative to this constant
require PROJECT_ROOT . '/inc/include1.php';

When executing from /soap/soap.php, if that file resides alongside script.php at the root level, $baseDir will still resolve correctly to the project root. If soap.php is nested deeper, you simply adjust the calculation of $baseDir relative to that specific script's location. The key is ensuring that $baseDir always points to the directory containing your inc, objects, and other shared folders, rather than the current execution script's directory.

Once this constant is defined, every subsequent file inclusion becomes stable. You replace fragile relative paths like ../inc/... with PROJECT_ROOT . '/inc/...'. This maintains your desired directory structure without relying on the caller's position in the filesystem tree.

There is a trade-off to consider regarding where you place this logic. If you define this constant in a file included by both script.php and soap.php, ensure it is defined only once. PHP will throw a fatal error if you attempt to redeclare an existing constant. A common pattern is to include a configuration bootstrapper at the very top of every entry point:

<?php

require_once PROJECT_ROOT . '/config/paths.php'; // Define paths here

// Rest of application logic

By centralizing the path resolution logic, you avoid duplication and ensure that moving your project to /var/www/html or mounting it from a network drive requires changing only the initial detection logic, not every include statement. This approach scales well as your project grows, preventing the codebase from becoming a maze of ../ traversals.

Our network