PHP fopen() fails and returns false
fopen() returns false instead of a file pointerShort 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.
Fix it fast
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');2Match what you see, then jump to its fix
- 1
Failed to open stream: Permission deniedThe web server user cannot read or write the file or folderFix - 2
Opening for write fails on an existing file, or the file is empty after the callThe mode refuses the file or empties itFix - 3
The warning mentions open_basediropen_basedir blocks the locationFix - 4
The 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
The echoed path is not the path you typedA Windows path with single backslashesFix
- 1
What does the error look like?
fopen() returns false instead of a file pointerWarning: fopen(...): Failed to open streamThe 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/data3Verify
The request now returns true and fopen() gives a resource.
var_dump($h);Confirmed by php.net
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 missing3Verify
Existing content is kept and new lines are added.
echo file_get_contents($path);Confirmed by php.net
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_basedirConfirmed by php.net
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_fopen2Fix
Turn it on in php.ini if you accept the risk, or fetch the URL another way, for example cURL.
allow_url_fopen = On3Verify
php -i shows allow_url_fopen => On => On and the URL opens.
php -i | grep allow_url_fopenConfirmed by php.net
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
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
PHP says it cannot open a file that is right there
Fatal error: Uncaught Error: Failed opening required 'vendor/autoload.php'
PHP could not find the file at the path it built.
2 causes, each with a fixBack end
file_get_contents() fails because the https:// wrapper is disabled
https:// wrapper is disabled in the server configuration by allow_url_fopen=0
allow_url_fopen lets PHP functions like file_get_contents() open URLs.
2 causes, each with a fixBack 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 fix