Skip to content
PHP Warning: include(): Failed opening '...'

PHP Warning: include(): Failed opening '...'

DodaTech 2 min read

The “include(): Failed opening” warning means PHP could not find the file you tried to include or require. Unlike a fatal error, the script continues with include but stops with require.

What It Means

When you call include('file.php'), PHP searches the directories listed in include_path (set in php.ini) plus the current script’s directory. If the file is not found in any of those locations, PHP emits this warning. The file path in the error message shows exactly what PHP tried to open.

Why It Happens

  • Relative path resolves incorrectly — the current working directory is not what you expect, so the relative path does not match.
  • File was moved or deleted — the file no longer exists at the referenced location.
  • Permissions issue — PHP cannot read the file because of filesystem permissions.
  • include_path not configured — the directory containing your file is not in PHP’s search path.
  • Symbolic link broken — the path points to a symlink whose target is missing.
  • File extension mismatch — you wrote include 'config.php' but the file is config.inc.php.

How to Fix It

1. Use __DIR__ for absolute paths

// ❌ Relative path depends on current working directory
include 'config/database.php';

// ✅ Always resolves from the current file's directory
include __DIR__ . '/config/database.php';

2. Verify the file exists

$path = __DIR__ . '/config/database.php';
if (!file_exists($path)) {
    die("File not found: $path");
}
include $path;

3. Set a custom include path

set_include_path(get_include_path() . PATH_SEPARATOR . '/var/www/lib');
include 'common.php'; // PHP will now search /var/www/lib too

4. Fix file permissions

chmod 644 /var/www/project/config/database.php
chown www-data:www-data /var/www/project/config/database.php

5. Check php.ini include_path

; In php.ini, ensure this includes your project directory
include_path = ".:/usr/share/php:/var/www/project"
What is the difference between include and require for this error?
include produces a warning and continues; require produces a fatal error and stops. Use require for critical files (like database config) and include for optional ones.
Why does my path work locally but not on the server?
Local development and production servers often have different directory structures. Always use __DIR__ or a defined constant like ROOT_PATH to avoid hard-coded paths.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro