Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP says it cannot open a file that is right there

ErrorWarning: include(config/app.php): Failed to open stream: No such file or directory

Short answer

PHP could not find the file at the path it built. Most often the path is relative and PHP resolved it from a different folder than you expected. Print the full path PHP tried, then build paths from __DIR__ instead of the working directory. If the file exists, check open_basedir next.

  • Applies to PHP 8.3, PHP 8.4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the absolute path PHP is really using and ask whether it exists. Run this from the same entry script that fails.

    <?php
    $p = __DIR__ . '/config/app.php';
    var_dump(getcwd(), $p, realpath($p), file_exists($p));
  2. 2Match what you see, then jump to its fix

    1. 1The same include works from one script and fails from another, or works in the browser and fails from cron or the CLI.A relative path is resolved from the wrong folderFix
    2. 2open_basedir restriction in effect. File(...) is not within the allowed path(s)open_basedir stops PHP from reaching the folderFix

What does the error look like?

  • Warning: include(config/app.php): Failed to open stream: No such file or directory
  • Fatal error: Uncaught Error: Failed opening required 'vendor/autoload.php'
  • Warning: fopen(): open_basedir restriction in effect

Cause 1 of 2

A relative path is resolved from the wrong folder

1Check

A path like 'config/app.php' is searched in include_path, then in the calling script's folder, then in the current working directory. A path starting with ./ or ../ is relative to the working directory only. The working directory changes with how the script is started, so the result changes too.

php -r "echo get_include_path(), PHP_EOL;"
php -r "echo getcwd(), PHP_EOL;"

2Fix

Build the path from the folder of the file that contains the include, using __DIR__, so it no longer depends on where the process started.

require __DIR__ . '/../vendor/autoload.php';
include __DIR__ . '/config/app.php';

3Verify

Run the script from two different folders and confirm it behaves the same.

php /var/www/app/public/index.php
cd / && php /var/www/app/public/index.php

Confirmed by php.net

Did this fix it?

Cause 2 of 2

open_basedir stops PHP from reaching the folder

1Check

open_basedir limits PHP to the listed directory trees. A file outside them cannot be opened even when it exists, and the warning names the allowed paths.

php -r "var_dump(ini_get('open_basedir'));"
grep -R "open_basedir" /etc/php/ /etc/apache2/ 2>/dev/null

2Fix

Move the file inside an allowed tree, or add its folder to open_basedir (separate entries with a colon on Linux). Reload PHP-FPM or Apache afterwards.

; php.ini or the pool/vhost config
open_basedir = /var/www/app:/tmp

3Verify

Repeat the request and check the warning is gone.

php -r "var_dump(ini_get('open_basedir'));"

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Never write include 'folder/file.php' with a bare relative name; always prefix with __DIR__ or a base-path constant.
  • Print realpath() of a path in the error message you throw, so the log shows the real location.
  • Run the same entry point from cron or the CLI once after each deploy, because the working directory differs there.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

CodeIgniter 4 cannot write its cache files

CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/

CodeIgniter 4's file cache needs a cache directory that the web server can really write to.

2 causes, each with a fix

Back end

CodeIgniter 3 loads the model but the property is undefined

The model name you are loading is the name of a resource that is already being used: user

CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.