fopen or file_put_contents fails because the target is an http:// URL
HTTP wrapper does not support writeable connectionsShort answer
PHP's http:// wrapper is read-only, so writing to a URL with file_put_contents() or fopen() in write mode always fails. Either you meant a file on disk and built the path from a URL, or you wanted to send data to another server. Use a filesystem path for files and a POST request with a stream context for sending.
Fix it fast
1Find out which cause you have
Print the exact first argument of the failing call. If it starts with http:// or https://, it is a URL, not a path.
var_dump($target); file_put_contents($target, $data);2Match what you see, then jump to its fix
What does the error look like?
HTTP wrapper does not support writeable connectionsfile_put_contents(http://example.com/cache/data.json) returns false
Cause 1 of 2
A file on disk is addressed with the site's URL
1Check
The manual's wrapper table lists HTTP as allowing reading but not writing or appending, and the PHP source rejects the modes a, w, x and +. Look for a base URL constant or $_SERVER['HTTP_HOST'] used where a directory path belongs.
grep -rn "file_put_contents\|fopen" src/ | grep -i 'http'2Fix
Build the target from a filesystem location such as __DIR__ and make sure the PHP user can write there. Keep the URL only for links shown to users.
$target = __DIR__ . '/cache/data.json';
file_put_contents($target, $data);3Verify
Run the code and confirm the file appears on disk.
ls -l cache/data.jsonConfirmed by php.net
Cause 2 of 2
The goal is to send data to another server
1Check
The manual says HTTP connections are read-only, and that POST and PUT requests are done with HTTP stream contexts.
grep -n 'stream_context_create' *.php2Fix
Send a POST with the http context options method, header and content, and read the answer with file_get_contents().
$opts = ['http' => [
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query(['var1' => 'some content']),
]];
$result = file_get_contents('http://example.com/submit.php', false, stream_context_create($opts));3Verify
The call returns the response body instead of a warning.
var_dump($result);Confirmed by php.net
How do I stop it coming back?
- Keep two separate settings in config: a base URL for links and a base directory for files.
- Create the target folder at deploy time and give the PHP user write access to it only.
- Check the return value of file_put_contents(); it returns false on failure.
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
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 fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix