PHP | hash_update_file() Function

PHP hash_update_file() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP hash_update_file() Function.

The built-in PHP hash_update_file() function updates the given file content with the hash context.

Syntax

Following below is the syntax to use this function -

hash_update_file ( HashContext $hcontext , string $filename [, resource $scontext = NULL ] ) : bool


Parameter Details

Sr.NoParameter & Description
1

HashContext context

The hash context that you get using hash_init().

2

filename

The file path, to get the content to be hashed.

3

scontext

Stream context as returned by stream_context_create().


Return Value

This built-in PHP functions returns true or false.

PHP Version

This built-in PHP function works from PHP version greater than 5.1.2.

Example1

Following example illustrates the use of the PHP hash_update_file() function -

<?php
   $hash_context = hash_init('md5');
   file_put_contents('file1.txt', 'Hello World'); 
   // create file file1.txt with content : 'Hello World'
   hash_update_file($hash_context, 'file1.txt');
   echo hash_final($hash_context);
?>

Output

When the above code is executed, it will produce the following result -

b10a8db164e0754105b7a99be72e3fe5

Example2

Following example illustrates the use of the built-in hash_update_file() function with the gost-crypto algo -

<?php
   $hash_context = hash_init('gost-crypto');
   file_put_contents('file1.txt', 'Hello World'); 
   // create file file1.txt with content : 'Hello World'
   hash_update_file($hash_context, 'file1.txt');
   echo hash_final($hash_context);
?>

Output

When the above code is executed, it will produce the following result -

75ed15d84df84291c67fe07bf234ac69e92a9c2a378ee62f342af739e829eba9


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be studying about the PHP hash_update_stream() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.