Ffile2fix
Sign in Get started

Fix "Failed to open stream: No such file or directory" in PHP

Warning: require(config.php): Failed to open stream: No such file or directory in /var/www/html/index.php on line 3

PHP tried to open a file or URL and could not. The text after the colon says why: No such file or directory (wrong path or missing file), Permission denied (filesystem permissions or SELinux), or an HTTP error for remote URLs. With require, PHP then stops with a fatal 'Failed opening required' error.

Also appears as: PHP Fatal error: Uncaught Error: Failed opening required 'config.php' (include_path='.:/usr/share/php') · Warning: include(/var/www/html/inc/header.php): Failed to open stream: Permission denied · Warning: file_get_contents(https://api.example.com): Failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found · Warning: fopen(/var/www/html/cache/data.json): Failed to open stream: No such file or directory

Common causes

  • A relative path resolved against the wrong working directory
  • The file was not uploaded, was renamed, or differs in case on a Linux server
  • File or directory permissions do not let the PHP user read or write it
  • open_basedir restricts PHP to certain directories
  • allow_url_fopen is Off, or the remote URL returned an error
  • SELinux blocks the web server from reading the path

How to fix it

  1. Use paths relative to the current file. Replace require 'config.php'; with require __DIR__ . '/config.php';. Plain relative paths depend on the current working directory and include_path, which differ between web requests, cron and CLI.
  2. Confirm the file exists. Run ls -l /var/www/html/config.php with the exact path from the message. Check the case of every folder and file name, because Linux paths are case-sensitive.
  3. Fix permissions. For Permission denied, make files readable by the PHP user, typically 644 for files and 755 for directories, owned by your deploy user or www-data. Writable folders like cache or uploads need write access for that user.
  4. Check open_basedir and SELinux. If phpinfo() shows open_basedir, the path must be inside one of the listed directories. On RHEL-based systems, run ls -Z and restorecon -Rv /var/www/html if SELinux contexts are wrong.
  5. Handle remote URLs properly. For file_get_contents('https://...') errors, check allow_url_fopen in phpinfo() and the HTTP status in the message. cURL or an HTTP client like Guzzle gives better error handling and timeouts.
  6. Restore Composer files. If the missing file is vendor/autoload.php, run composer install in the project root. The vendor folder is usually not committed to Git and must be installed on each server.

Robust include paths

<?php
// Resolves relative to this file, not the working directory
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/../vendor/autoload.php';

$path = __DIR__ . '/cache/data.json';
if (!is_readable($path)) {
    throw new RuntimeException("Cannot read $path");
}
$data = file_get_contents($path);

How to stop it happening again

  • Build every include path from __DIR__ or a defined base path constant
  • Run composer install as part of deployment
  • Set ownership and permissions in your deploy script instead of by hand
  • Check is_readable() or is_writable() before working with runtime files

Frequently asked questions

Why does it work in the browser but fail in cron?

Cron runs PHP from a different working directory, often your home folder, so relative paths point somewhere else. Using __DIR__ fixes this.

Why did 'failed' become 'Failed' in the message?

PHP 8.0 capitalized the message. 'failed to open stream' and 'Failed to open stream' are the same error.

Should I chmod 777 to fix Permission denied?

No. 777 lets any user on the server modify the files. Give write permission only to the directories PHP must write to, owned by the PHP user.