{"record":{"id":"6d0ebf2b1e2c4511","repo":"Intervention/image","slug":"failed-to-build-stream","errorCode":null,"errorMessage":"Failed to build stream","messagePattern":"Failed to build stream","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\StreamException","httpStatus":null,"severity":"error","filePath":"src/Traits/CanBuildStream.php","lineNumber":43,"sourceCode":"            is_string($data) => function (mixed $data) {\n                $stream = fopen('php://temp', 'r+');\n\n                if ($stream === false) {\n                    throw new StreamException('Failed to build stream from string');\n                }\n\n                fwrite($stream, $data);\n                return $stream;\n            },\n            default => throw new InvalidArgumentException(\n                'Unable to create stream from ' . gettype($data) . '. Use only null, string or resource.',\n            ),\n        };\n\n        $stream = $buildStrategy($data);\n\n        if ($stream === false) {\n            throw new StreamException('Failed to build stream');\n        }\n\n        $rewind = rewind($stream);\n\n        if ($rewind === false) {\n            throw new StreamException('Failed to rewind stream');\n        }\n\n        return $stream;\n    }\n}\n","sourceCodeStart":25,"sourceCodeEnd":55,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Traits/CanBuildStream.php#L25-L55","documentation":"After buildStreamOrFail() picks a construction strategy, a final check verifies the returned handle is not false; if it is, this generic StreamException is thrown. In practice the only strategy that can return false here (rather than throw its own specific exception) is the null branch whose fopen('php://temp', 'r+') failed. Like error 464 it signals an environment failure to create a stream, just via the shared post-check.","triggerScenarios":"Calling buildStreamOrFail(null) — directly or through APIs that default to null, such as File/decoder paths that construct an empty working stream — while php://temp cannot be opened. Correlates with memory_limit exhaustion, a full or unwritable system temp directory, or a sandbox (e.g. seccomp/open_basedir hardening) blocking temp stream creation.","commonSituations":"Hardened shared hosting or containers where /tmp is mounted read-only or size-limited; PHP-FPM workers already at memory_limit; systemd PrivateTmp with a full tmpfs; rare stream wrappers disabled via disable_functions-like hardening.","solutions":["Free memory / raise memory_limit before the operation.","Verify the temp filesystem is writable and has space: check sys_temp_dir and tmpfs mounts; clear or remount /tmp.","Catch Intervention\\Image\\Exceptions\\StreamException at the call site and surface an environment-level error message.","If reproducible, test fopen('php://temp', 'r+') in isolation to confirm the PHP runtime itself is broken, then fix the runtime/container config."],"exampleFix":"// before\n// /tmp full -> fopen('php://temp') fails\n$file = \\Intervention\\Image\\File::create(); // StreamException: Failed to build stream\n\n// after\n// free temp space / raise memory_limit first, then\n$file = \\Intervention\\Image\\File::create();","handlingStrategy":"try-catch","validationCode":"// sanity-check the temp stream factory before heavy work\nif (fopen('php://temp', 'r+') === false) {\n    throw new \\RuntimeException('php://temp unavailable - check memory_limit and tmp dir');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $file = \\Intervention\\Image\\File::create();\n} catch (\\Intervention\\Image\\Exceptions\\StreamException $e) {\n    // environment issue: surface it, do not retry blindly\n    throw new \\RuntimeException('Cannot create temp stream: ' . $e->getMessage());\n}","preventionTips":["Keep memory_limit and temp disk space healthy in image-processing environments.","Add health checks that verify fopen('php://temp', 'r+') works in containers/sandboxes.","Treat StreamException from stream construction as infrastructure failure, not input failure."],"tags":["php","intervention-image","stream","memory-limit","tmp","environment"],"backgroundTag":"stream-open-failed","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}