PHP stream_get_meta_data() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_get_meta_data() returns an associative array of information about an open stream, such as its mode, whether it is seekable, and whether a read timed out.

The keys include timed_out, blocked, eof, wrapper_type, stream_type, mode, unread_bytes, seekable, and uri. Use it when you need to inspect how a stream behaves without reading its payload.

Syntax

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

PHP
$meta = stream_get_meta_data($stream);
CallWhat it does
stream_get_meta_data($stream)Returns the metadata array for the open stream.
stream_get_meta_data($stream)["seekable"]true when you can rewind or seek on that stream.
stream_get_meta_data($stream)["mode"]Shows the open mode string, such as "r+" or "rb".

Metadata describes the stream handle, not the file contents. Some keys are more useful for network streams (timed_out, unread_bytes), while seekable and mode matter for files and memory streams too.

Library declarations
PHP
stream_get_meta_data(resource $stream): array

Parameters

ParameterDescription
streamRequired. The open stream resource to inspect.

Return Value

Return ValueDescription
arrayAssociative array of stream metadata keys and values.

PHP Examples

Example 1 — Inspect a Memory Stream

Open php://memory and print a few stable metadata fields.

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

echo "mode=", $meta["mode"], PHP_EOL;
echo "seekable=", $meta["seekable"] ? "yes" : "no", PHP_EOL;
echo "eof=", $meta["eof"] ? "yes" : "no", PHP_EOL;
fclose($stream);

How It Works

  1. fopen() opens php://memory in r+ mode.
  2. stream_get_meta_data() returns the metadata array.
  3. mode matches the open mode; seekable is yes for this wrapper.
  4. eof is no because we have not finished a read that reached the end.

Example 2 — See EOF After Reading Everything

Write a short string, read it all, then check the eof flag again.

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

$meta = stream_get_meta_data($stream);
echo "eof=", $meta["eof"] ? "yes" : "no", PHP_EOL;
fclose($stream);

How It Works

  1. Two characters are written and the pointer is rewound.
  2. stream_get_contents() reads to the end.
  3. stream_get_meta_data() now reports eof as true.
  4. This is a quick way to confirm you have consumed the stream.

Example 3 — Read wrapper_type and uri

Print which wrapper owns the stream and the uri PHP associates with it.

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

echo "wrapper_type=", $meta["wrapper_type"], PHP_EOL;
echo "uri=", $meta["uri"], PHP_EOL;
fclose($stream);

How It Works

  1. The memory stream uses PHP's built-in php:// wrapper family.
  2. wrapper_type reports PHP for this handle.
  3. uri echoes the path you opened: php://memory.
  4. These fields help you log which kind of stream you are dealing with.

Frequently Asked Questions

It returns an associative array describing an open stream: mode, seekable, eof, timeout flags, wrapper type, and more.
Start with mode, seekable, eof, timed_out, and uri. They answer how the stream was opened and whether reading finished or timed out.
No. It only describes the stream handle. Use stream_get_contents(), fread(), or stream_get_line() to read data.
After a read or write that hit the limit set by stream_set_timeout(). Check it when working with network streams.
Yes. It works on file streams, memory streams, sockets, and other stream resources.

Next: stream_get_transports()

stream_get_transports() returns the names of the socket transports available in the curren.

stream_get_transports() 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