{"record":{"id":"a9db37e875c1fb68","repo":"thephpleague/flysystem","slug":"invalid-stream-provided-expected-stream-resource","errorCode":null,"errorMessage":"Invalid stream provided, expected stream resource, received {type}","messagePattern":"Invalid stream provided, expected stream resource, received (.+?)","errorType":"exception","errorClass":"InvalidStreamProvided","httpStatus":null,"severity":"error","filePath":"src/Filesystem.php","lineNumber":262,"sourceCode":"                is_array($publicUrl) => new ShardedPrefixPublicUrlGenerator($publicUrl),\n                default => new PrefixPublicUrlGenerator($publicUrl),\n            };\n        }\n\n        if ($this->adapter instanceof PublicUrlGenerator) {\n            return $this->adapter;\n        }\n\n        return null;\n    }\n\n    /**\n     * @param mixed $contents\n     */\n    private function assertIsResource($contents): void\n    {\n        if (is_resource($contents) === false) {\n            throw new InvalidStreamProvided(\n                \"Invalid stream provided, expected stream resource, received \" . gettype($contents)\n            );\n        } elseif (($type = get_resource_type($contents)) !== 'stream') {\n            throw new InvalidStreamProvided(\n                \"Invalid stream provided, expected stream resource, received resource of type \" . $type\n            );\n        }\n    }\n\n    /**\n     * @param resource $resource\n     */\n    private function rewindStream($resource): void\n    {\n        if (ftell($resource) !== 0 && stream_get_meta_data($resource)['seekable']) {\n            rewind($resource);\n        }\n    }","sourceCodeStart":244,"sourceCodeEnd":280,"githubUrl":"https://github.com/thephpleague/flysystem/blob/b277b5dc3d56650b68904117124e79c851e12376/src/Filesystem.php#L244-L280","documentation":"Filesystem::writeStream() validates its $contents argument with assertIsResource(): it must be a PHP stream resource. If is_resource() is false (you passed a string, null, array, object, bool, int), an InvalidStreamProvided exception is thrown with the received type from gettype(). This is a developer API-contract error, not an I/O error.","triggerScenarios":"Calling $filesystem->writeStream($path, file_get_contents($file)) (string); passing a PSR-7 StreamInterface object (e.g. Guzzle stream); passing null/false from a failed fopen(); forwarding a variable that was never opened as a stream.","commonSituations":"Refactoring write() code to writeStream() without switching file_get_contents to fopen; consuming HTTP bodies and passing response stream objects; forgot that write() takes strings but writeStream() takes resources.","solutions":["Pass a stream resource: $stream = fopen($file, 'rb'); then $filesystem->writeStream($path, $stream);.","If you have a string, use writeStream with php://memory (stream_get_contents) or simply call write().","If you have a PSR-7 StreamInterface, call ->detach() to obtain the underlying resource.","Check fopen() for false before using it so a failed open doesn't cascade into this error."],"exampleFix":"// before\n$filesystem->writeStream('dest.txt', file_get_contents('/tmp/source.txt')); // string -> throws\n\n// after\n$stream = fopen('/tmp/source.txt', 'rb');\nif ($stream === false) {\n    throw new RuntimeException('Unable to open source file.');\n}\n$filesystem->writeStream('dest.txt', $stream);\nfclose($stream);","handlingStrategy":"type-guard","validationCode":"if ( ! is_resource($contents)) {\n    throw new InvalidArgumentException('writeStream() requires a stream resource; use write() for strings.');\n}","typeGuard":"/**\n * Narrow a value to a valid open stream resource for Flysystem writeStream().\n */\nfunction isStreamResource(mixed $value): bool\n{\n    return is_resource($value) && get_resource_type($value) === 'stream';\n}","tryCatchPattern":"use League\\Flysystem\\InvalidStreamProvided;\n\ntry {\n    $filesystem->writeStream($path, $contents);\n} catch (InvalidStreamProvided $e) {\n    // programmer error: fix the call site, do not retry\n    throw new InvalidArgumentException($e->getMessage(), 0, $e);\n}","preventionTips":["Use write() for strings and writeStream() only for fopen() resources — never mix them up.","Always check fopen() for false before passing its result onward.","Unwrap PSR-7 bodies with ->detach() before writing them via writeStream().","Add the isStreamResource() guard at API boundaries that accept user-supplied streams."],"tags":["php","flysystem","stream","write-stream","invalid-argument","type-error"],"backgroundTag":"invalid-stream-resource","analyzedSha":"b277b5dc3d56650b68904117124e79c851e12376","analyzedAt":"2026-08-17T04:28:35.741Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}