PHP include - failed to open stream: No such file
When refactoring a static HTML site into a dynamic PHP application, separating shared components like headers and navigation files is a standard move. You might start with a simple index.php in your root directory that successfully pulls in /test/assets/header.php using a relative path. However, the moment you create a new page inside a subdirectoryâsay /test/company/index.phpâand try to include that same file using an absolute path like /test/assets/header.php, PHP throws a failed to open stream: No such file or directory error.
This behavior is not a bug in PHP; it is a direct consequence of how the server interprets file paths relative to the current working directory versus the document root. When you use a leading slash (/) in a path string, PHP treats it as an absolute path starting from the filesystem root (e.g., /home/user/test/assets/header.php on a Linux box). Unless your web server's document root is actually configured exactly at that top-level slash, which is rare for security reasons, the file simply cannot be found.
The solution lies in understanding how include and require resolve paths. By default, these functions are relative to the script currently executing, not the URL you see in the browser. In your working example at /test/index.php, the path assets/header.php is resolved relative to the current directory of that script. Since both files reside directly under /test, the relative path works perfectly.
When you move to /test/company/index.php, the current directory becomes /test/company. If you switch to an absolute path starting with /test/..., you are telling PHP to look at the root of the filesystem, not your project's root. This mismatch causes the file to vanish from view.
The correct approach is almost always to use relative paths that account for the depth of the current script. From /test/company/index.php, you need to traverse up one level to reach the assets folder. The code should look like this:
<?php include '../../assets/header.php'; ?>
Alternatively, if you prefer not to calculate directory levels manually every time, you can use dirname(__DIR__) or simply ensure your includes are relative to the web root by adjusting the path structure. However, relying on relative paths is generally more robust because it decouples your PHP code from the specific server configuration (like changing the document root).
A common pitfall arises when developers attempt to bypass this by using a full URL-like path or an absolute filesystem path directly in include, such as http://example.com/assets/header.php. PHP will reject these attempts with a "URL file-access is disabled" error. This restriction exists because allowing scripts to access arbitrary URLs via file includes opens the door to remote code execution and severe security vulnerabilities.
If you find yourself constantly adjusting paths, consider moving your shared assets into a dedicated folder structure that minimizes directory traversal logic, or leverage an autoloader if you are using object-oriented PHP. For larger projects, especially those migrating from static HTML, frameworks like Laravel handle this path resolution automatically through their file system helpers and strict directory conventions. You can explore how modern frameworks manage asset loading at https://laravelcompany.com to see best practices for keeping your include paths clean and secure without manual calculation.
Ultimately, the error message is a clear indicator that the path does not match the actual location of the file on the server's filesystem relative to where the script is running. Stick to relative paths from the current script location, or use __DIR__ to construct them dynamically, and you will avoid these stream opening failures.