PHP hash_update_stream() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Hash

Definition

In PHP, hash_update_stream() reads data from an open stream, such as a file handle, and adds it to a hashing context.

It returns how many bytes it added, and you can limit how many to read.

Syntax

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

PHP
$bytes = hash_update_stream($context, $stream);
CallWhat it does
hash_update_stream($ctx, $fp)Reads to the end of the stream.
hash_update_stream($ctx, $fp, 5)Reads at most 5 bytes.

The stream moves forward as it is read. Call rewind() first if it was written to.

Library declarations
PHP
hash_update_stream(HashContext $context, resource $stream, int $length = -1): int

Parameters

ParameterDescription
contextRequired. A HashContext from hash_init().
streamRequired. An open stream resource, such as from fopen().
lengthOptional. Maximum bytes to read. -1 (default) reads to the end.

Return Value

Return ValueDescription
intThe number of bytes added to the hash.

PHP Examples

Example 1 — Hash a Stream

Write to memory, rewind, and hash it.

PHP
<?php
$fp = fopen('php://memory', 'r+');
fwrite($fp, 'Hello, CodeToFun!');
rewind($fp);

$ctx   = hash_init('sha256');
$bytes = hash_update_stream($ctx, $fp);

echo "Bytes: ", $bytes, PHP_EOL;
echo "Same as hash(): ", hash_final($ctx) === hash('sha256', 'Hello, CodeToFun!') ? "yes" : "no", PHP_EOL;

fclose($fp);

How It Works

  1. Open a memory stream. Write the text to it.
  2. Rewind to the start. Reading begins there.
  3. Call `hash_update_stream()`. It returns the byte count.
  4. Finish and compare. The hash equals hash() of the text.

Example 2 — Limit the Bytes

Read only the first 5 bytes.

PHP
<?php
$fp = fopen('php://memory', 'r+');
fwrite($fp, 'Hello, CodeToFun!');
rewind($fp);

$ctx   = hash_init('sha256');
$bytes = hash_update_stream($ctx, $fp, 5);

echo "Bytes: ", $bytes, PHP_EOL;
echo "Matches 'Hello': ", hash_final($ctx) === hash('sha256', 'Hello') ? "yes" : "no", PHP_EOL;

fclose($fp);

How It Works

  1. Pass 5 as the length. Only 5 bytes are read.
  2. Check the count. It returns 5.
  3. Finish. The context holds Hello.
  4. Compare. It equals hash() of Hello.

Example 3 — Read in Two Steps

Call it twice to continue where it stopped.

PHP
<?php
$fp = fopen('php://memory', 'r+');
fwrite($fp, 'Hello, CodeToFun!');
rewind($fp);

$ctx = hash_init('sha256');
echo "Chunk 1: ", hash_update_stream($ctx, $fp, 7), " bytes", PHP_EOL;
echo "Chunk 2: ", hash_update_stream($ctx, $fp), " bytes", PHP_EOL;
echo "Matches hash(): ", hash_final($ctx) === hash('sha256', 'Hello, CodeToFun!') ? "yes" : "no", PHP_EOL;

fclose($fp);

How It Works

  1. Read the first 7 bytes. The stream moves forward.
  2. Read the rest. No length means read to the end.
  3. Count the bytes. 7 plus 10 is 17.
  4. Compare. Two reads give the same hash as one.

Frequently Asked Questions

It reads data from an open stream and adds it to a hashing context, then returns the number of bytes it added.
hash_update_stream(HashContext $context, resource $stream, int $length = -1): int.
Pass a length. For example 5 reads at most 5 bytes. Call it again to continue from where it stopped.
The stream was already at its end. Use rewind() to go back to the start before you call it.
Use it for streams that are not plain files, such as php://memory, sockets or uploads you already opened.

Back to PHP Hash Functions

Browse every hash function tutorial.

Hash functions →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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