Current Location: Home> Latest Articles> How to use hash_final in PHP with file_get_contents?

How to use hash_final in PHP with file_get_contents?

gitbox 2025-05-27

File hashing calculations in PHP are a common requirement, especially when verifying file integrity or encryption processing. This article will introduce how to combine hash_final and file_get_contents to implement hash calculation of files.

1. Use file_get_contents to get file content

First, we need to use the file_get_contents function to read the content from the file. The file_get_contents function can return the contents of a file as a string. This function is especially suitable for reading smaller files, which can consume a lot of memory if the file is very large. For large files, it is recommended to use chunked reading.

 $file_path = 'path/to/your/file.txt'; // Replace with the path of the file
$file_contents = file_get_contents($file_path);

In this example, we read the contents of the file.txt file and store it in the variable $file_contents .

2. Initialize the hash algorithm using hash_init

Next, we need to select a hashing algorithm to calculate the hash value of the file. PHP provides a variety of hashing algorithms, such as md5 , sha256 , etc. Before calculating the hash value, we need to initialize the hash algorithm first.

 $algorithm = 'sha256';  // usesha256Algorithm performs hash calculation
$context = hash_init($algorithm);

Here, we have chosen the sha256 algorithm, which you can replace with other algorithms such as md5 according to your needs.

3. Pass the file content to hash_update

Next, we pass the file contents returned by file_get_contents to the hash_update function to calculate the hash value in the initialized hash context.

 hash_update($context, $file_contents);

At this time, the content of the file has been updated to hash calculation.

4. Use hash_final to calculate the final hash value

Once the file content is updated, we can use the hash_final function to calculate the final hash value. hash_final will return the hash result of the entire file content.

 $hash_value = hash_final($context);
echo "The hash value of the file is: " . $hash_value;

At this point, $hash_value will store the hash value of the file and output the value.

Complete sample code

Here is a complete PHP code example showing how to combine hash_final and file_get_contents to calculate the hash value of a file:

 <?php
$file_path = 'path/to/your/file.txt'; // Replace with the path of the file
$file_contents = file_get_contents($file_path);

$algorithm = 'sha256';  // usesha256algorithm
$context = hash_init($algorithm);

hash_update($context, $file_contents);

$hash_value = hash_final($context);
echo "The hash value of the file is: " . $hash_value;
?>

5. Other precautions

  1. File size : For larger files, file_get_contents may cause insufficient memory, especially when reading large files. Consider using fopen and fread to read files in chunks.

  2. Security : While md5 and sha1 are still widely used, these algorithms have been proven to be vulnerable to collision attacks. If scenarios with higher security requirements are involved, it is recommended to use a safer algorithm, such as sha256 or sha512 .

  3. Performance : The performance of hash calculations is related to the algorithms and file sizes used. For applications with high performance requirements, it may be necessary to select the appropriate algorithm according to the specific needs.