Ad Space — Top Banner

Warning: file_get_contents() failed to open stream

PHP Programming Language

Severity: Moderate

What Does This Error Mean?

This warning means PHP tried to read a file or URL with file_get_contents() but could not open it. For files: the file does not exist, the path is wrong, or PHP does not have permission to read it. For URLs: the server is unreachable, the URL is wrong, or allow_url_fopen is disabled in php.ini. The function returns false when it fails — check for that and handle the error.

Affected Models

  • PHP 5.x
  • PHP 7.x
  • PHP 8.x
  • All PHP versions

Common Causes

  • The file path is wrong — absolute vs relative path confusion, or a typo in the filename
  • The file does not exist at the specified location
  • PHP does not have read permission for the file or folder
  • Trying to fetch a URL but allow_url_fopen is disabled in php.ini
  • Network error or the remote server is unreachable or returned an error

How to Fix It

  1. Check the file path. Use an absolute path to avoid confusion: __DIR__ . '/myfile.txt' gives the directory of the current PHP file.

    Relative paths depend on the working directory, which can differ between CLI and web server contexts. Always prefer absolute paths.

  2. Verify the file exists: if (file_exists($path)) { $content = file_get_contents($path); } else { echo 'File not found: ' . $path; }

    This gives you a clear error message instead of a PHP warning.

  3. Check PHP's file permission to the folder and file. The web server runs as a specific user (often 'www-data' on Linux). That user must have read access.

    On Linux: chmod 644 myfile.txt gives the owner write access and everyone else read access. chmod 755 for directories.

  4. For fetching URLs, check php.ini: allow_url_fopen must be set to On. Or better, use cURL instead which is more reliable for HTTP requests.

    In php.ini: allow_url_fopen = On. On shared hosting, you may not be able to change this — use cURL as an alternative.

  5. Always check the return value of file_get_contents(). It returns false on failure: $content = file_get_contents($url); if ($content === false) { handle error; }

    You can also suppress the warning with @ prefix and check the return: $content = @file_get_contents($path); — but this hides the warning, which makes debugging harder.

When to Call a Professional

This warning is always fixable yourself. Check the file path, file permissions, and php.ini settings. For URL fetching in production applications, use cURL instead of file_get_contents — it gives better error handling.

Frequently Asked Questions

What is the difference between file_get_contents() and cURL for fetching URLs?

file_get_contents() is simpler — just one line to fetch a URL. But it requires allow_url_fopen=On and has limited error handling. cURL is more powerful — you can set timeouts, follow redirects, send headers, handle authentication, and get detailed error information. For simple reads, file_get_contents is fine. For anything beyond a basic GET request, use cURL.

What does __DIR__ mean in PHP?

__DIR__ is a PHP magic constant that contains the absolute path of the directory where the current PHP file is located. Using __DIR__ . '/config.json' always points to config.json in the same folder as the PHP file, regardless of what directory PHP was launched from. This is safer than relative paths like './config.json', which depend on the current working directory.

Why does my code work locally but fail on the server?

Usually because of different file permissions or different directory structures. On a local machine you might run PHP as yourself (with full permissions). On a server, PHP runs as a limited web server user. Check that the server user has read access to the file. Also check that the file actually exists on the server — it may exist locally but not have been deployed.