try/catch does not catch a missing require or include file
Warning: include(config.php): Failed to open stream: No such file or directoryShort answer
In PHP 8 a failed require throws an Error you can catch with catch (Throwable) or catch (Error). A failed include only raises a warning, so try/catch ignores it; convert warnings to ErrorException with set_error_handler. Before PHP 8.0 a failed require was an uncatchable E_COMPILE_ERROR.
Fix it fast
1Find out which cause you have
Run the include in a tiny script with the exact path and see which of the two shapes you get: a warning with the script continuing, or a fatal stop. That decides which cause below applies.
<?php include __DIR__ . '/missing.php'; echo "still running\n"; require __DIR__ . '/missing.php'; echo "never reached\n";2Match what you see, then jump to its fix
What does the error look like?
Warning: include(config.php): Failed to open stream: No such file or directorytry/catch around include() does not catch anythingthe script stops with a fatal error when a required file is missing
Cause 1 of 2
require throws an Error, and the catch type is too narrow
1Check
The manual says a failed require produces an Error exception from PHP 8.0, where earlier versions raised an E_COMPILE_ERROR. Error does not extend Exception, so catch (Exception) misses it.
php -v2Fix
Catch Throwable, or Error. Use the same block for any other fatal error you want to survive.
try {
require __DIR__ . '/config/local.php';
} catch (Throwable $e) {
error_log($e->getMessage());
// fall back to defaults here
}3Verify
Rename the file and run the script. The message is logged and the script continues.
php test.phpConfirmed by php.net
Cause 2 of 2
include only raises a warning, so there is nothing to catch
1Check
include produces an E_WARNING when it fails and execution continues. A warning is not an exception, so try/catch around it does nothing.
grep -rn "include" src/ | head2Fix
Turn warnings into ErrorException with set_error_handler, wrap the include in try/catch, and restore the previous handler afterwards. Return false for errors your error_reporting level excludes.
set_error_handler(function (int $no, string $str, string $file, int $line): bool {
if (!(error_reporting() & $no)) {
return false;
}
throw new ErrorException($str, 0, $no, $file, $line);
});
try {
include __DIR__ . '/optional.php';
} catch (ErrorException $e) {
error_log($e->getMessage());
} finally {
restore_error_handler();
}3Verify
Run it with the file missing: the exception is caught and the script goes on.
php test.phpConfirmed by php.net
How do I stop it coming back?
- Use require for files the app cannot run without and let it stop loudly; use include only for optional files.
- Do not put @ in front of include to hide a missing file; the error still happens and you lose the message.
- Log the Throwable message with the path, so a deploy that forgot a file is found quickly.
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack 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 fix