PHP hash_update_file() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Hash

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.

PHP
hash_update_file($context, $path);
CallWhat 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
PHP
hash_update_file(HashContext $context, string $filename, ?resource $stream_context = null): bool

Parameters

ParameterDescription
contextRequired. A HashContext from hash_init().
filenameRequired. The path to the file.
stream_contextOptional. A stream context, used with URL wrappers.

Return Value

Return ValueDescription
booltrue 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
<?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);

How It Works

  1. Create a temp file. It holds a known text.
  2. Add the file to the context. hash_update_file() reads it.
  3. Finish the hash. You get the hex value.
  4. Compare with `hash()`. The text gives the same result.

Example 2 — Text Plus a File

Mix strings and files in one hash.

PHP
<?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

  1. Add a text header first. It goes in as text.
  2. Add the file next. Its contents follow the header.
  3. Finish. The hash covers both parts in order.
  4. Compare. It equals hash() of the joined text.

Example 3 — Hash Several Files in Order

Two files become one hash.

PHP
<?php
$a = tempnam(sys_get_temp_dir(), 'hu_');
$b = tempnam(sys_get_temp_dir(), 'hu_');
file_put_contents($a, 'AAA');
file_put_contents($b, 'BBB');

$ctx = hash_init('sha256');
foreach ([$a, $b] as $path) {
    hash_update_file($ctx, $path);
}

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

unlink($a);
unlink($b);

How It Works

  1. Make two files. They hold AAA and BBB.
  2. Add them in a loop. Order is a then b.
  3. Finish. One hash covers both files.
  4. Compare. It equals the hash of AAABBB.

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.

Next: hash_update_stream()

hash_update_stream() adds data from an open stream to a hash.

hash_update_stream() tutorial →

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