Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If the same PHP include works on one page but fails on another, the first suspect is the path. The pages may start execution from different directories, use different working directories, or load a file through a different include chain. Anchor every application file to the directory of the file that contains the statement:

require_once __DIR__ . '/includes/header.php';

Use ../ when the target is above that directory. This removes most ambiguity caused by the current working directory, the calling script, and PHP’s include_path.

A small directory example

Consider this layout:

site/
├── includes/
│   └── header.php
├── index.php
└── admin/
    └── dashboard.php

This statement may work in index.php:

include 'includes/header.php';

When admin/dashboard.php executes it, PHP may search for site/admin/includes/header.php rather than site/includes/header.php. The source line is identical; the execution context is not.

Use paths based on the file containing the statement:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
// index.php
require_once __DIR__ . '/includes/header.php';

// admin/dashboard.php
require_once __DIR__ . '/../includes/header.php';

__DIR__ is the directory of the current PHP file and has no trailing slash (except for the filesystem root). PHP documents it as equivalent to dirname(__FILE__). See the magic constants documentation.

Filesystem paths are not browser URLs

A PHP include reads a file on the server. It does not request a URL from the browser.

  • /includes/header.php in HTML is a URL rooted at the website’s document root.
  • include '/includes/header.php'; on a Unix-like server is a filesystem path rooted at the server’s filesystem, usually meaning /includes/header.php, not your site’s public directory.
  • require_once __DIR__ . '/includes/header.php'; constructs a filesystem path from the PHP file’s real location.

Use URL paths for browser assets:

<link rel="stylesheet" href="/assets/site.css">

Use filesystem paths for PHP code and templates.

What PHP is actually resolving

A bare name such as include 'header.php'; can be affected by the current working directory, the calling script, and configured include_path. Explicit paths beginning with ./, ../, a filesystem root, or a Windows drive letter follow different lookup rules. The PHP include documentation describes these lookup and scope details.

The browser’s visible URL is not a reliable filesystem reference. A router or front controller may execute /public/index.php for URLs such as /products and /admin/users. Build paths from the actual PHP file or a known application root, not from the URL.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

The reliable __DIR__ pattern

File in the same directory

require_once __DIR__ . '/config.php';

File in a child directory

require_once __DIR__ . '/templates/header.php';

File one directory above

require_once __DIR__ . '/../bootstrap.php';

In the last example, PHP starts in the directory represented by __DIR__, moves up one level with .., then looks for bootstrap.php. Every included file should anchor its own dependencies this way.

include, require, and the _once variants

Construct Failure behavior Typical use
include Emits a warning and normally continues Optional fragment, banner, or widget
require Stops with a fatal error Mandatory configuration or bootstrap
include_once Optional include, loaded at most once per request Optional code that may be reached through several paths
require_once Mandatory include, loaded at most once per request Autoloader, classes, functions, database configuration

PHP documents the stronger failure behavior of require in its require documentation. Use require_once for essential files:

require_once __DIR__ . '/../config/database.php';

Use include when absence is an acceptable condition:

include __DIR__ . '/optional-banner.php';

require_once prevents duplicate loading; it does not correct an incorrect path.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Debug the exact path instead of guessing

  1. Read the complete warning or fatal error. It often prints the path PHP attempted and the configured include_path. Do not hide it with @include; error suppression removes useful diagnostics. See PHP’s require documentation for failure behavior.
  2. Print both execution contexts.
    echo '<pre>';
    echo 'CWD: ' . getcwd() . PHP_EOL;
    echo '__DIR__: ' . __DIR__ . PHP_EOL;
    echo '__FILE__: ' . __FILE__ . PHP_EOL;
    echo '</pre>';

    getcwd() reports the process working directory; __DIR__ and __FILE__ identify the source file.

  3. Inspect the constructed target.
    $path = __DIR__ . '/../includes/header.php';
    var_dump($path);
    var_dump(file_exists($path));
    var_dump(is_readable($path));

    file_exists() checks the supplied path; it does not search include_path. is_readable() tests whether the PHP process can read it. See file_exists() and is_readable().

  4. Resolve the canonical name.
    var_dump(realpath($path));

    realpath() returns a canonical absolute path or false when resolution fails. See realpath().

  5. Check what has already loaded.
    print_r(get_included_files());

    This includes nested files loaded by all four include/require constructs. See get_included_files().

  6. Enable diagnostics only during development.
    error_reporting(E_ALL);
    ini_set('display_errors', '1');

    error_reporting(E_ALL) enables all PHP error levels. Log errors instead of displaying server paths on production sites; see error_reporting().

Nested includes need their own anchors

Suppose the project is:

project/
├── public/index.php
└── app/
    ├── views/layout.php
    └── helpers/html.php

The entry script can load the layout with:

// public/index.php
require_once __DIR__ . '/../app/views/layout.php';

Inside layout.php, this is fragile:

require_once 'helpers/html.php';

Use the layout’s directory instead:

// app/views/layout.php
require_once __DIR__ . '/../helpers/html.php';

A dependency should not rely on whichever page happened to include its parent.

If the path is correct, check these causes

Filename case and spelling

Linux filesystems are commonly case-sensitive. Includes/Header.php and includes/header.php can be different files. Verify directory names, extensions, punctuation, spaces, and that deployment actually copied the file. Code that works on a case-insensitive development machine can fail on a Linux server.

Permissions and ownership

A file can exist but be unreadable by the web-server or PHP-FPM user. On the server, inspect both the file and every parent directory:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
ls -l /path/to/project/includes/header.php
namei -l /path/to/project/includes/header.php

The PHP process needs permission to traverse parent directories and read the file. Fix ownership and apply the minimum required permissions; do not use chmod -R 777 . as a blanket remedy.

Different include_path or runtime

Inspect the configured search path:

echo get_include_path();
var_dump(ini_get('include_path'));

Apache, PHP-FPM, CLI, cron, containers, virtual hosts, and PHP versions can use different php.ini settings. A browser request and a scheduled command may also start with different working directories. Explicit __DIR__ paths are safer than relying on configuration. PHP defines include_path in its core directives documentation.

Restrictions imposed by the environment

If the path exists but access still fails, check open_basedir, container mounts, PHP-FPM pool restrictions, SELinux or AppArmor policies, chroots, and symlink restrictions:

var_dump(ini_get('open_basedir'));

The include is never reached

An earlier parse error or fatal error can stop execution before the include line. Read the first error in the log, not just the last visible symptom.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

The file loads but produces no visible output

An included file may contain no output, a false conditional, output buffering, markup hidden by CSS, or PHP code outside valid <?php tags. It may return a value without echoing it.

Includes inherit the variable scope where they occur. An include inside a function does not automatically receive globals:

$title = 'Dashboard';

function renderPage(): void
{
    include __DIR__ . '/template.php';
}

Pass required data deliberately:

function renderPage(string $title): void
{
    include __DIR__ . '/template.php';
}

See the scope discussion in PHP’s include documentation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choosing a long-term path strategy

Approach When it fits Trade-off
__DIR__ paths Small and medium projects; templates and procedural files Moving a file may require updating its relative path
Project-root constant Legacy applications with many deep directories Bootstrap must run first and define the constant once
Composer autoloading Modern namespaced classes and dependencies Does not automatically replace arbitrary template or configuration includes
include_path Controlled legacy libraries Environment-dependent and can select an unexpected same-named file
$_SERVER['DOCUMENT_ROOT'] Traditional web-only layouts May be absent in CLI and usually is only the public web root
Hard-coded absolute path One fixed deployment Breaks across machines, operating systems, containers, and releases

A project-root constant can reduce repeated traversal:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
// config/bootstrap.php
define('PROJECT_ROOT', dirname(__DIR__));

// elsewhere
require_once PROJECT_ROOT . '/app/config.php';

Define it from one known bootstrap location and avoid names likely to collide with libraries. For classes, load Composer once:

require_once __DIR__ . '/../vendor/autoload.php';

Then use namespaced classes rather than manually including each class file.

Final troubleshooting checklist

  1. Read the full PHP warning or fatal error.
  2. Confirm filename and capitalization.
  3. Print __DIR__, __FILE__, and getcwd().
  4. Construct the target with __DIR__.
  5. Test realpath($path).
  6. Test file_exists($path).
  7. Test is_readable($path).
  8. Check parent-directory permissions and ownership.
  9. Compare include_path between web, CLI, and production.
  10. Check open_basedir and hosting restrictions.
  11. Verify the include is reached before another error.
  12. Check variable scope, conditions, buffering, and CSS.
  13. Inspect get_included_files() for duplicate or unexpected loads.
  14. Use require_once for mandatory dependencies and include only for optional fragments.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.