Failed to Open Stream: No Such File or Directory

Warning: fopen(data/output.csv): Failed to open stream: No such file or directory

Quick Answer

The file or directory path does not exist. Create the directory first or check that the file path is correct.

Why This Happens

This warning occurs when PHP cannot open a file because the path does not exist. Unlike require errors, this affects functions like fopen, file_get_contents, and file_put_contents. The directory may not exist, the filename may be wrong, or a relative path may resolve differently than expected.

The Problem

// The 'data' directory doesn't exist
$handle = fopen('data/output.csv', 'w');

The Fix

$dir = __DIR__ . '/data';
if (!is_dir($dir)) {
    mkdir($dir, 0755, true);
}
$handle = fopen($dir . '/output.csv', 'w');

Step-by-Step Fix

  1. 1

    Verify the path exists

    Check that every directory in the path exists. Use is_dir() to verify and mkdir() with the recursive flag to create missing directories.

  2. 2

    Use absolute paths

    Replace relative paths with absolute paths using __DIR__ to avoid working directory confusion.

  3. 3

    Check error reporting

    Enable error reporting and use is_readable() or is_writable() to check access before attempting file operations.

  4. 4

    Match the message to the cause

    The text after the colon tells you the cause: 'No such file or directory' means a wrong or missing path, 'Permission denied' means the PHP user cannot read or write it, and 'HTTP request failed' means a remote URL that returned an error or is blocked by allow_url_fopen or the network.

Got the actual stack trace?

Paste it into our free AI explainer to get the cause and the fix for your specific case — no signup, nothing to install.

Explain my error

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free