hash_update_stream() – Populate an Active Hash Context from an Open Stream (PHP)

The article explains the PHP function hash_update_stream(), detailing its signature, parameters (context, handle, length), return value, and provides a complete example that reads data from a temporary file stream, updates an MD5 hash context, and outputs the resulting hash.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
hash_update_stream() – Populate an Active Hash Context from an Open Stream (PHP)

The PHP function hash_update_stream() reads data from an open stream and feeds it into an active hash context, allowing incremental hashing of large data sources.

Signature:

int hash_update_stream(resource $context, resource $handle[, int $length = -1])

Parameters:

context : the hash context resource returned by hash_init().

handle : an open file handle representing the stream to read from.

length (optional): the maximum number of bytes to copy from the handle; default -1 means read until EOF.

Return value: the number of bytes actually read from the handle and added to the hash context.

Example:

<?php
$fp = tmpfile();
fwrite($fp, 'The quick brown fox jumped over the lazy dog.');
rewind($fp);
$ctx = hash_init('md5');
hash_update_stream($ctx, $fp);
echo hash_final($ctx);
?>

The script outputs the MD5 hash of the string, for example:

5c6ffbdd40d9556b73a21e63c3e0e904
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPHashingMD5Streamhash_update_stream
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.