PHP hash_update_file() Function

Definition
In PHP, hash_update_file() reads a file and adds its contents to a hashing context.
Use it to build one hash from several files, or from text plus a file.
Syntax
Call hash_update_file() with the values you need. Start with this line.
hash_update_file($context, $path);
| Call | What it does |
|---|
hash_update_file($ctx, 'a.txt') | Adds the contents of a.txt. |
hash_update_file($ctx, 'b.txt') | Adds the contents of b.txt next. |
The file is read in chunks, so large files use little memory.
Library declarations
hash_update_file(HashContext $context, string $filename, ?resource $stream_context = null): bool
Parameters
| Parameter | Description |
|---|
| context | Required. A HashContext from hash_init(). |
| filename | Required. The path to the file. |
| stream_context | Optional. A stream context, used with URL wrappers. |
Return Value
| Return Value | Description |
|---|
| bool | true on success, false if the file cannot be read. |
PHP Examples
Example 1 — Add a File to a Hash
Hash the contents of one file.
<?php
$file = tempnam(sys_get_temp_dir(), 'hu_');
file_put_contents($file, 'Hello, CodeToFun!');
$ctx = hash_init('sha256');
hash_update_file($ctx, $file);
$digest = hash_final($ctx);
echo $digest, PHP_EOL;
echo "Same as hash(): ", $digest === hash('sha256', 'Hello, CodeToFun!') ? "yes" : "no", PHP_EOL;
unlink($file);
027bd65edfc2f0baf383db119d8a4a1a3c70697c717c9179a6903411b8109467
Same as hash(): yes
How It Works
- Create a temp file. It holds a known text.
- Add the file to the context.
hash_update_file() reads it. - Finish the hash. You get the hex value.
- Compare with `hash()`. The text gives the same result.
Example 2 — Text Plus a File
Mix strings and files in one hash.
<?php
$file = tempnam(sys_get_temp_dir(), 'hu_');
file_put_contents($file, 'Hello, CodeToFun!');
$ctx = hash_init('sha256');
hash_update($ctx, 'HEADER:');
hash_update_file($ctx, $file);
echo "Match: ", hash_final($ctx) === hash('sha256', 'HEADER:Hello, CodeToFun!') ? "yes" : "no", PHP_EOL;
unlink($file);
How It Works
- Add a text header first. It goes in as text.
- Add the file next. Its contents follow the header.
- Finish. The hash covers both parts in order.
- Compare. It equals
hash() of the joined text.
Frequently Asked Questions
It reads a file and adds its contents to a hashing context created with hash_init().
hash_update_file(HashContext $context, string $filename, ?resource $stream_context = null): bool.
hash_file() hashes one file at once. hash_update_file() lets you combine files and text into one hash.
false, plus a warning. Check the result before you finish the hash.
Yes. The hash depends on the order. Hashing a then b gives a different result than b then a.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
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