PHP stream_get_contents() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_get_contents() reads the remaining data from an open stream and returns it as a string, starting at the current position unless you pass an offset.

It is the stream twin of file_get_contents() for an already-open handle. You can limit how many bytes to read, or jump to an offset first. When the read fails, it returns false.

Syntax

Call stream_get_contents() with the values you need. Start with this line.

PHP
$text = stream_get_contents($stream);
CallWhat it does
stream_get_contents($stream)Reads from the current position to the end and returns the string.
stream_get_contents($stream, 5)Reads at most 5 bytes from the current position.
stream_get_contents($stream, null, 2)Seeks to offset 2, then reads the rest.

Unlike fread(), you do not have to loop. After a successful full read to the end, further reads usually return an empty string. Always check for false when a failure matters.

Library declarations
PHP
stream_get_contents(resource $stream, ?int $length = null, int $offset = -1): string|false

Parameters

ParameterDescription
streamRequired. An open stream resource, such as one from fopen() or a php:// wrapper.
lengthOptional. Maximum number of bytes to read. null (the default) means read until the end.
offsetOptional. Seek to this position before reading. -1 (the default) means stay at the current position.

Return Value

Return ValueDescription
stringThe data that was read (may be an empty string at EOF).
falseReturned when the read fails.

PHP Examples

Example 1 — Read a Memory Stream

Write text into php://memory, rewind, then read everything with stream_get_contents().

PHP
<?php
$stream = fopen("php://memory", "r+");
fwrite($stream, "hello stream");
rewind($stream);

echo stream_get_contents($stream), PHP_EOL;
fclose($stream);

How It Works

  1. fopen() opens an in-memory stream for reading and writing.
  2. fwrite() stores the text and leaves the pointer at the end.
  3. rewind() moves back to the start so the next read sees the data.
  4. stream_get_contents() returns the full string "hello stream".

Example 2 — Limit the Number of Bytes

Pass a length so only the first few characters come back.

PHP
<?php
$stream = fopen("php://temp", "r+");
fwrite($stream, "ABCDEFGH");
rewind($stream);

echo stream_get_contents($stream, 4), PHP_EOL;
echo stream_get_contents($stream), PHP_EOL;
fclose($stream);

How It Works

  1. Eight letters are written into a temp stream.
  2. The first stream_get_contents() call asks for only 4 bytes and prints ABCD.
  3. The pointer now sits after D, so the second call reads the rest: EFGH.
  4. This pattern is useful when you want a header first and the body next.

Example 3 — Read From an Offset

Use the third argument to jump into the middle without a separate seek call.

PHP
<?php
$stream = fopen("php://memory", "r+");
fwrite($stream, "0123456789");

echo stream_get_contents($stream, 3, 4), PHP_EOL;
fclose($stream);

How It Works

  1. Digits 0–9 are written into the stream.
  2. stream_get_contents($stream, 3, 4) seeks to offset 4 and reads 3 bytes.
  3. That slice is the characters "456".
  4. You do not need rewind() here because the offset argument places the pointer for you.

Frequently Asked Questions

It reads data from an open stream into a string. By default it reads from the current position to the end.
file_get_contents() opens a path for you. stream_get_contents() reads from a stream you already opened with fopen(), fsockopen(), or similar.
The file pointer is usually at the end after fwrite(). Call rewind() or pass an $offset so reading starts where the data is.
Yes. Pass $length for a maximum byte count, and optionally $offset to start at a given position.
It returns false when the read fails. Treat false as an error, and remember that a successful empty read returns "" instead.

Next: stream_get_filters()

stream_get_filters() returns a list of the stream filter names currently registered and av.

stream_get_filters() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful