relative path in require_once doesn't work

relative path in require_once doesn't work

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

Stefan Bogdanescu

Founder & Senior Architect

Relative Paths in require_once Are Not Magic

When a project starts working with absolute paths but fails immediately when you switch to relative paths, it usually signals a misunderstanding of how PHP resolves file locations. The error message failed to open stream: No such file or directory is the standard response from PHP when its internal path resolution logic cannot locate the target file. In the scenario described, the application structure places utility files in an include folder, while class definitions reside in a sibling class folder. When index.php requests include/include.php, that script attempts to load resources using paths like '../class/database.php'. This relative path fails because the calculation of "parent directory" changes depending on where the current script resides within the web root.

The core issue lies in how PHP determines the starting point for a relative path. When you execute require_once '../class/database.php' from inside /Applications/MAMP/htdocs/otsg/include/include.php, PHP interprets the .. as "go up one directory level from the current file's location." This takes you to /Applications/MAMP/htdocs/otsg. From there, it looks for a folder named class. While this logic seems sound on paper, the error persists because the include_path configuration is interfering with the lookup process or the path string itself is being interpreted differently than expected.

In the provided code snippet, ini_set('include_path', ...) attempts to modify the include path by appending the project root and subdirectories. However, modifying the global include_path via ini_set() is rarely a robust solution for application logic because it can conflict with PHP's default behavior, especially when running under different environments like MAMP or production servers. The error log shows the effective include path includes /Applications/MAMP/htdocs/otsg/include/. When PHP encounters a relative path in a require_once statement, it first checks the script's directory and then appends the entries from the include_path if the file is not found locally.

The specific failure occurs because the relative path '../class/database.php' is being resolved against the directory of include.php, not the project root. If you are running this code in a browser context, the working directory might be different than expected, or the ini_set call might have altered the search order such that PHP skips the local relative resolution and tries to find ../class/database.php inside the appended include paths, which obviously doesn't exist as a literal path string.

To resolve this without relying on absolute paths, you must ensure that every file knows exactly where it is relative to the project root. The most reliable method is to calculate the base path dynamically at runtime. Instead of hardcoding '../class/database.php', you can define a constant in config.inc.php that resolves to the actual directory structure.

// In config.inc.php
define('BASE_PATH', dirname(__DIR__));

Then, update your includes to use this base:

require_once BASE_PATH . '/class/database.php';
require_once BASE_PATH . '/class/user.php';
require_once BASE_PATH . '/encryption.php';

This approach works because dirname(__DIR__) always resolves to the parent of the current script's directory, allowing you to construct paths relative to a known anchor point (the project root) rather than relying on fragile relative navigation that breaks when moving files around.

If you are already using Laravel or need a framework-agnostic solution that scales better, consider leveraging autoloading. The LaravelCompany platform offers robust solutions for managing dependencies and file structures without manually writing require_once statements everywhere. Their documentation provides examples on how to set up namespaces so that classes like Database and User can be autoloaded based on their namespace rather than a physical path string, effectively removing the headache of relative path calculations entirely.

By shifting from manual relative paths to either dynamic base path constants or modern autoloading strategies, you eliminate the fragility introduced by directory traversal operators. This ensures that your application remains consistent regardless of where the entry point file is located within the web server hierarchy.

Our network