Include php files when they are in different folders

Include php files when they are in different folders

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

Stefan Bogdanescu

Founder & Senior Architect

Include php files when they are in different folders

When you structure a web application by placing the main entry point and shared resources like CSS, images, and utility functions in a root directory, including those assets from within that same directory is trivial. A simple <include> or require_once statement works perfectly because the relative path is straightforward. The real complexity arises when you introduce a sub-application or a distinct module, such as a "blog" section, that lives in its own folder with its own index.php.

In this scenario, your main root directory contains folders like css, functions, and images. Your blog folder sits alongside it. When you attempt to include a file from the functions folder while executing code inside the blog's index.php, the include path must traverse up one level to reach the parent directory. While using <include ../functions/myfile.php> resolves the immediate issue of finding the file, it quickly creates a cascading dependency problem if that included file relies on other resources located in the main root or deeper sub-directories.

The fundamental issue here is not the include statement itself, but the lack of a consistent reference point for file resolution. When you manually adjust paths with ../, you are coupling your code to the physical filesystem layout. This makes maintenance difficult and brittle. If you move the blog folder or decide to nest it deeper in the future, every single include path that relies on the parent directory breaks.

To solve this without spaghetti paths, you need to establish a clear boundary between the application's root and the current execution context. In PHP, the most robust way to handle cross-directory includes is to define a constant at the very top of your entry points—typically index.php in both the main site and the blog—that represents the base path of your project.

You can achieve this by using dirname(__DIR__). The __DIR__ magic constant holds the directory name of the file containing the code. By wrapping it with dirname(), you get the parent directory relative to that file. If your main index.php is in /var/www/html/ and your blog folder is /var/www/html/blog/, setting a constant like this ensures every file knows exactly where the project root lives:

define('ROOT_PATH', dirname(__DIR__));

Once ROOT_PATH is defined, you can reference any resource relative to that anchor point rather than relying on arbitrary up-level traversals. If your functions/myfile.php includes a CSS file from the main root, the code inside myfile.php becomes:

require ROOT_PATH . '/css/style.css';
include ROOT_PATH . '/functions/utils/helper.php';

This approach decouples the logic from the directory structure. Whether the blog is in a folder named blog, cms, or nested three levels deep, as long as you define ROOT_PATH correctly in every entry point (the main site and the blog), the rest of your codebase remains stable.

There are trade-offs to consider. Defining this constant means it must be included before any other logic runs, which usually requires placing it at the top of every file that acts as an entry point or a loader. However, this is a standard pattern in larger PHP projects. If you are using a framework like Laravel, this manual path resolution is unnecessary because the framework handles the autoloading and base path logic for you. For custom CMS builds or legacy systems, however, establishing a shared root constant is the industry-standard solution to managing includes across multiple folders.

If your project eventually scales beyond simple file inclusion, consider adopting an autoloader. A class-based autoloader can map namespaces directly to directory structures without needing to manually include every script. This eliminates the need for require statements entirely, allowing you to focus on logic rather than path management. For complex custom CMS architectures, this shift often simplifies the codebase significantly over time.

Our network