PHP stream_set_timeout() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_set_timeout() sets how long a stream will wait during a read or write before giving up.

Pass whole seconds and optional microseconds. After a timeout, reads may return false or incomplete data, and stream_get_meta_data() marks timed_out as true. Use it on network sockets so a silent remote host cannot hang your script forever.

Syntax

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

PHP
$ok = stream_set_timeout($stream, 5, 0);
CallWhat it does
stream_set_timeout($stream, 10)Waits up to 10 seconds for stream operations.
stream_set_timeout($stream, 0, 500000)Waits up to half a second using microseconds.
stream_set_timeout($stream, 2, 0)Sets a two-second timeout and returns true on success.

The timeout is seconds plus microseconds. Check the return value, and after a slow read inspect timed_out through stream_get_meta_data(). socket_set_timeout() is an older alias of the same function.

Library declarations
PHP
stream_set_timeout(resource $stream, int $seconds, int $microseconds = 0): bool

Parameters

ParameterDescription
streamRequired. An open stream, often from stream_socket_client() or fsockopen().
secondsRequired. Whole seconds to wait.
microsecondsOptional. Extra microseconds to wait. Defaults to 0.

Return Value

Return ValueDescription
trueThe timeout was applied.
falseThe timeout could not be set.

PHP Examples

Example 1 — Set a Five-Second Timeout

Apply a timeout on a php://temp stream and print the boolean result.

PHP
<?php
$stream = fopen('php://temp', 'r+');
$ok = stream_set_timeout($stream, 5, 0);
var_dump($ok);
fclose($stream);

How It Works

  1. fopen('php://temp') gives a safe offline stream.
  2. stream_set_timeout() stores a five-second wait limit.
  3. 0 microseconds means no extra fraction of a second.
  4. true means PHP accepted the setting.

Example 2 — Seconds and Microseconds Together

Set a short timeout of 1.5 seconds by combining seconds and microseconds.

PHP
<?php
$stream = fopen('php://temp', 'r+');
$ok = stream_set_timeout($stream, 1, 500000);
echo $ok ? 'timeout set to 1.5s' : 'failed', PHP_EOL;
fclose($stream);

How It Works

  1. 1 second plus 500000 microseconds equals 1. 5 seconds.
  2. Microseconds let you choose sub-second waits.
  3. The function returns true when the setting is stored.
  4. Keep timeouts short enough for your script or job limits.

Example 3 — Check timed_out Metadata

Set a timeout, then read the timed_out flag before any slow network read happens.

PHP
<?php
$stream = fopen('php://temp', 'r+');
stream_set_timeout($stream, 2, 0);

$meta = stream_get_meta_data($stream);
echo 'timed_out before read: ', $meta['timed_out'] ? 'yes' : 'no', PHP_EOL;
echo 'timeout ready for network streams', PHP_EOL;

fclose($stream);

How It Works

  1. stream_set_timeout() stores the limit on the stream.
  2. timed_out stays false until an operation actually times out.
  3. stream_get_meta_data() reads that flag.
  4. On real sockets, check timed_out after a failed or partial read.

Frequently Asked Questions

It sets the read/write timeout on a stream so operations stop waiting after the limit you choose.
The wait limit is seconds plus microseconds. For example 1 and 500000 means one and a half seconds.
No. It only stops waiting. Check the operation result and the timed_out key from stream_get_meta_data().
Remote servers can stall. A timeout keeps your PHP script from hanging forever on fgets() or fread().
No. socket_set_timeout() is an alias of stream_set_timeout(). Both names call the same function.

Next: stream_set_write_buffer()

stream_set_write_buffer() sets the size of the write buffer for an open stream.

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