Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP fopen() fails and returns false

Errorfopen() returns false instead of a file pointer

Short answer

fopen() returns false and raises an E_WARNING when it cannot open the path. Read that warning first. The usual causes are a path the web server user cannot read or write, a mode that refuses the file, an open_basedir limit, a URL with allow_url_fopen off, or unescaped Windows backslashes.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Turn warnings on for one run and read the text after fopen(). It names the exact reason. Then print the full path you pass in.

    <?php
    ini_set('display_errors', '1');
    error_reporting(E_ALL);
    $path = __DIR__ . '/data/log.txt';
    echo $path, PHP_EOL;
    $h = fopen($path, 'a');
  2. 2Match what you see, then jump to its fix

    1. 1Failed to open stream: Permission deniedThe web server user cannot read or write the file or folderFix
    2. 2Opening for write fails on an existing file, or the file is empty after the callThe mode refuses the file or empties itFix
    3. 3The warning mentions open_basediropen_basedir blocks the locationFix
    4. 4The warning says the URL wrapper is disabled, and the path starts with http:// or ftp://A URL is used while allow_url_fopen is offFix
    5. 5The echoed path is not the path you typedA Windows path with single backslashesFix

What does the error look like?

  • fopen() returns false instead of a file pointer
  • Warning: fopen(...): Failed to open stream
  • The file is not created, or it is empty after the script runs

Cause 1 of 5

The web server user cannot read or write the file or folder

1Check

PHP runs as the web server's user, which is often not the account you log in with. is_writable() answers for that user, so run it from a web request, not from your shell.

<?php
var_dump(is_writable(__DIR__ . '/data'));

2Fix

Make the web server user the owner of the folder that holds the file, and only that folder. Replace www-data with the user your PHP process runs as.

sudo chown www-data:www-data /var/www/app/data

3Verify

The request now returns true and fopen() gives a resource.

var_dump($h);

Confirmed by php.net

Did this fix it?

Cause 2 of 5

The mode refuses the file or empties it

1Check

Mode x fails if the file already exists. Mode w truncates an existing file to zero length, and a creates the file if it is missing and appends.

var_dump(file_exists($path));

2Fix

Pick the mode that matches what you want: r to read, a to append, w only when the old content may be lost, x only for a new file.

$h = fopen($path, 'a');   // append, create if missing

3Verify

Existing content is kept and new lines are added.

echo file_get_contents($path);

Confirmed by php.net

Did this fix it?

Cause 3 of 5

open_basedir blocks the location

1Check

open_basedir limits PHP to a directory tree; any file outside it is refused, and symlinks are resolved first, so a link cannot avoid the limit.

<?php
echo ini_get('open_basedir');

2Fix

Move the file inside an allowed directory, or add its directory to open_basedir in the php.ini or pool config. On Windows, separate paths with a semicolon; elsewhere use a colon.

open_basedir = "/var/www/app:/tmp"

3Verify

ini_get shows the new value after a PHP restart and fopen() succeeds.

php -i | grep open_basedir

Confirmed by php.net

Did this fix it?

Cause 4 of 5

A URL is used while allow_url_fopen is off

1Check

Opening an http or ftp URL needs allow_url_fopen. It defaults to on, but hosts often turn it off, and it can be set only in php.ini.

php -i | grep allow_url_fopen

2Fix

Turn it on in php.ini if you accept the risk, or fetch the URL another way, for example cURL.

allow_url_fopen = On

3Verify

php -i shows allow_url_fopen => On => On and the URL opens.

php -i | grep allow_url_fopen

Confirmed by php.net

Did this fix it?

Cause 5 of 5

A Windows path with single backslashes

1Check

The manual says to escape any backslashes in a Windows path or use forward slashes. Print the path and see what PHP really received.

echo $path;

2Fix

Use forward slashes, or escape each backslash.

$h = fopen("c:/folder/resource.txt", "r");
$h = fopen("c:\\folder\\resource.txt", "r");

3Verify

The printed path matches the real one and the file opens.

var_dump(file_exists('c:/folder/resource.txt'));

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Always test the return value and handle false before you read or write.
  • Build paths from __DIR__ so they do not depend on the working directory.
  • Keep writable data in one folder owned by the web server user.

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 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.