{"record":{"id":"4699252156e05723","repo":"composer/composer","slug":"the-url-file-could-not-be-written-to-destinat","errorCode":null,"errorMessage":"The \"{url}\" file could not be written to {destination}: {error}","messagePattern":"The \"(.+?)\" file could not be written to (.+?): (.+?)","errorType":"http","errorClass":"TransportException","httpStatus":null,"severity":"error","filePath":"src/Composer/Util/Http/CurlDownloader.php","lineNumber":216,"sourceCode":"        if ($copyTo !== null) {\n            $bodyTarget = $copyTo.'~';\n        } else {\n            $bodyTarget = 'php://temp/maxmemory:524288';\n        }\n\n        $errorMessage = '';\n        set_error_handler(static function (int $code, string $msg) use (&$errorMessage): bool {\n            if ($errorMessage) {\n                $errorMessage .= \"\\n\";\n            }\n            $errorMessage .= Preg::replace('{^fopen\\(.*?\\): }', '', $msg);\n\n            return true;\n        });\n        $bodyHandle = fopen($bodyTarget, 'w+b');\n        restore_error_handler();\n        if (false === $bodyHandle) {\n            throw new TransportException('The \"'.Url::sanitize($url).'\" file could not be written to '.($copyTo ?? 'a temporary file').': '.$errorMessage);\n        }\n\n        curl_setopt($curlHandle, CURLOPT_URL, $url);\n        curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, false);\n        curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, 10);\n        curl_setopt($curlHandle, CURLOPT_TIMEOUT, max((int) ini_get(\"default_socket_timeout\"), 300));\n        curl_setopt($curlHandle, CURLOPT_WRITEHEADER, $headerHandle);\n        curl_setopt($curlHandle, CURLOPT_FILE, $bodyHandle);\n        curl_setopt($curlHandle, CURLOPT_ENCODING, \"\"); // let cURL set the Accept-Encoding header to what it supports\n        curl_setopt($curlHandle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);\n\n        if ($attributes['ipResolve'] === 4) {\n            curl_setopt($curlHandle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);\n        } elseif ($attributes['ipResolve'] === 6) {\n            curl_setopt($curlHandle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);\n        }\n\n        if ($attributes['retries'] > 0) {","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/composer/composer/blob/6ffc1177404d0c50119c22dde6564a380f4a82c9/src/Composer/Util/Http/CurlDownloader.php#L198-L234","documentation":"TransportException thrown in CurlDownloader::initDownload() when fopen() of the response body destination fails. The destination is either the user-supplied $copyTo.'~' (when saving a file) or a php://temp stream; the captured PHP fopen error message is appended so the root cause (disk full, permissions, invalid path) is visible.","triggerScenarios":"initDownload() reaches line 213 fopen($bodyTarget,'w+b') and it returns false; the set_error_handler captured $errorMessage. Happens when $copyTo points at a non-writable/unreachable directory, disk is full, or the temp stream cannot be allocated.","commonSituations":"COMPOSER_HOME or cache dir on a read-only filesystem; vendor dir owned by another user; disk/quota exhausted; very long path on Windows; SELinux denying writes; running Composer as a user without write permission to the target directory.","solutions":["Verify write permission and free space on the target directory and COMPOSER_HOME: `df -h` and `touch <dir>/test` then remove it.","Set COMPOSER_HOME to a writable location: `export COMPOSER_HOME=/tmp/composer` (or a persistent writable dir).","Fix ownership/permissions: `chown -R $USER <dir>` / `chmod -R u+w <dir>`.","Clear the Composer cache (`composer clear-cache`) in case the cache dir is the culprit."],"exampleFix":"// before: target dir not writable by current user\n//   composer install   # => could not be written to /var/www/vendor/...~: Permission denied\n// after\n//   sudo chown -R $USER:$USER /var/www\n//   composer install","handlingStrategy":"validation","validationCode":"$dest = $copyTo ?? sys_get_temp_dir();\nif (!is_writable(dirname($dest))) {\n    throw new \\RuntimeException('Destination dir not writable: '.dirname($dest));\n}","typeGuard":"function isWritableTarget(?string $copyTo): bool {\n    return $copyTo === null || is_writable(dirname($copyTo));\n}","tryCatchPattern":"try {\n    $downloader->get($url, [], $copyTo);\n} catch (\\Composer\\Downloader\\TransportException $e) {\n    if (str_contains($e->getMessage(), 'could not be written to')) {\n        // fix perms / free disk / set COMPOSER_HOME, then retry\n    }\n    throw $e;\n}","preventionTips":["Ensure COMPOSER_HOME and the cache/vendor dirs are writable by the Composer user.","Monitor free disk space in CI before running installs.","Use absolute paths for copyTo to avoid cwd ambiguity."],"tags":["filesystem","permissions","disk-space","download","transport"],"analyzedSha":"6ffc1177404d0c50119c22dde6564a380f4a82c9","analyzedAt":"2026-08-07T00:01:08.491Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}