PHP says it cannot open a file that is right there
Warning: include(config/app.php): Failed to open stream: No such file or directoryShort 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.
Fix it fast
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));2Match what you see, then jump to its fix
- 1
The 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
open_basedir restriction in effect. File(...) is not within the allowed path(s)open_basedir stops PHP from reaching the folderFix
- 1
What does the error look like?
Warning: include(config/app.php): Failed to open stream: No such file or directoryFatal 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.phpConfirmed by php.net
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/null2Fix
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:/tmp3Verify
Repeat the request and check the warning is gone.
php -r "var_dump(ini_get('open_basedir'));"Confirmed by php.net
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
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack 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 fixBack 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 fixBack end
CodeIgniter 3 stops with "Your system folder path does not appear to be set correctly"
503 Service Unavailable
CodeIgniter 3's front controller resolves $system_path and then checks that the result is a directory.
3 causes, each with a fix