Maaz Zindani

Loading the index…

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

fopen or file_put_contents fails because the target is an http:// URL

ErrorHTTP wrapper does not support writeable connections

Short 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.

  • 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

    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);
  2. 2Match what you see, then jump to its fix

    1. 1The path in the warning begins with http:// even though you only want to save a cache or upload file.A file on disk is addressed with the site's URLFix
    2. 2The URL is a real remote endpoint and the code tries to push a body to it.The goal is to send data to another serverFix

What does the error look like?

  • HTTP wrapper does not support writeable connections
  • file_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.json

Confirmed by php.net

Did this fix it?

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' *.php

2Fix

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

Did this fix it?

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

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.