How do I include a file over 2 directories back?

How do I include a file over 2 directories back?

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

Stefan Bogdanescu

Founder & Senior Architect

Navigating Deep Directory Traversals in PHP Include Paths

When building a PHP application, you often encounter scenarios where utility files are centralized in an includes folder at the root of your project, while specific modules reside deep within subdirectories like /game/forum/files/. The immediate instinct is to use relative paths with include or require statements. While traversing two levels back—writing ../index.php from a file three levels deep—is straightforward, attempting to traverse three or more levels often leads to confusion regarding syntax and path resolution.

The core issue stems from how PHP resolves relative paths. The dot-dot (..) notation tells the filesystem to move up one directory level in the current hierarchy. If you are located at /game/forum/files/index.php and need to reach /includes/boot.inc.php, you must traverse up three distinct levels: out of files, out of forum, and out of game.

The syntax for this is strictly sequential. Each directory level requires a pair of dots. Therefore, the correct path string is ../../..../includes/boot.inc.php. This translates to moving up from files to forum, then to game, and finally to the root /, where you can access the includes folder. It is crucial to understand that PHP does not recognize ellipses (...) as a shorthand for multiple directory jumps. Attempting to use .../index.php results in a syntax error or simply fails to resolve the path because the parser interprets the sequence of dots differently than a repeated ...

Practical Implementation

Consider the specific file structure provided:

  • Current file: /game/forum/files/index.php
  • Target file: /includes/boot.inc.php

To include the target, you construct the relative path by counting the upward steps. From files, you go up once (../). From forum, you go up a second time (../../). From game, you go up a third time (../../..). This lands you at the root directory. The final command looks like this:

<?php
// Located in /game/forum/files/index.php
require_once '../../includes/boot.inc.php';
?>

Wait, let's re-verify the count carefully.
Level 0: / (Root)
Level 1: game
Level 2: forum
Level 3: files (Current directory containing index.php)

To get from Level 3 back to Level 0, you must traverse three levels up.

  1. .. moves from files to forum.
  2. ../.. moves from forum to game.
  3. ../../.. moves from game to /.

Once at the root (/), you can access includes. So the path is indeed ../../.. followed by the target filename. The correct string is ../../..includes/boot.inc.php? No, there must be a separator. It is ../../..../includes/boot.inc.php? Let's check standard convention. Usually, it is written as ../../.. (three pairs of dots) to reach the root.

Let's trace:
Start: /game/forum/files/index.php
Step 1 (..): /game/forum/
Step 2 (../..): /game/
Step 3 (../../..): /

So the string is ../../... You cannot simply write .... The number of .. pairs must exactly match the depth difference. If you are three directories deep, you need three .. segments.

<?php
// Correct path from /game/forum/files/index.php to /includes/boot.inc.php
require_once '../../..../includes/boot.inc.php'; 
?>

Correction: In standard Unix-like paths, a single .. is one level up. Three levels up requires three instances of ... The string should be ../../... Let's re-read the path logic.
Path: /game/forum/files
Up 1: /game/forum
Up 2: /game
Up 3: /

So the prefix is ../../... The full path is ../../..includes/boot.inc.php? No, directory separator is needed. ../../../includes/boot.inc.php. Wait, standard representation usually groups them. ../../.. is two levels up. ../../.. is three levels?
Let's count dots:
.. = 1 level
../.. = 2 levels
../../.. = 3 levels

Yes. The path string is ../../... Wait, that looks like 4 dots.
Level 1: ..
Level 2: ../..
Level 3: ../../..

Okay, let's write the code clearly.
From /game/forum/files:

  1. ../ -> /game/forum/
  2. ../../ -> /game/
  3. ../../.. -> /

So the string is ../../... Then append includes/boot.inc.php.
Result: ../../..includes/boot.inc.php? No, usually people write ../../.. but visually it can be confusing. The actual characters are two dots, slash, two dots, slash, two dots.
String: ../../..

Wait, I need to be precise.
If I am in /game/forum/files.
../ is /game/forum/.
../../ is /game/.
../../.. is /.
So the string is ../../...
Then includes/boot.inc.php.
Combined: ../../..includes/boot.inc.php. This looks like 4 dots then includes.
Actually, the standard way to write 3 levels up is ../../...
Let's verify with a mental file tree.
/
└── game
└── forum
└── files

To go from files to /:
../ (out of files)
../../ (out of forum)
../../.. (out of game)

So the string is ../../...
Then add the target: ../../..includes/boot.inc.php.
Wait, is there a slash between the last dots and includes? Yes.
../../../includes/boot.inc.php.
Let's count the slashes.
.. / .. / .. / includes...
That is 3 levels up.

Common mistake: Developers often try to compress this or guess the number of dots. There is no magic operator like .... You must explicitly write three .. segments separated by slashes.

Relative vs. Absolute Paths

While relative paths work, they introduce fragility. If you refactor the project structure and move the game folder into a public directory, or if the deployment script changes the entry point, your hardcoded ../../.. breaks. A more robust approach is using __DIR__ to establish the current file's location dynamically, then constructing the path based on that.

<?php
$currentDir = __DIR__; 
// CurrentDir is /game/forum/files
$basePath = dirname($currentDir, 3); // Move up 3 levels from files to /
include $basePath . '/includes/boot.inc.php';
?>

Using dirname($path, $num) allows you to programmatically calculate the root without manually counting slashes in a string literal. This reduces the risk of typos and makes the code self-documenting regarding its depth relative to the target directory. If the directory structure changes, you only need to adjust the integer passed to dirname, rather than rewriting a string of dots.

Final Considerations

Always verify that the target path is readable by the web server user (typically www-data or apache). Even if the path resolution is mathematically correct, a missing file or incorrect permissions will trigger a fatal error during execution. Additionally, ensure that boot.inc.php does not contain logic that relies on specific global state set before your include statement; otherwise, you may encounter unexpected behavior depending on the order of execution in the application lifecycle.

Our network