Current Location: Home> Latest Articles> How to Solve High Memory Usage When Handling Large Files Using the hash_update_stream Function?

How to Solve High Memory Usage When Handling Large Files Using the hash_update_stream Function?

M66 2025-06-11

When working with large files in PHP, we often face the problem of excessive memory usage. Especially when calculating the file's hash value (such as MD5, SHA1, etc.), loading the entire file into memory at once can lead to huge memory consumption, possibly exceeding memory limits and causing the program to crash. To avoid this, PHP provides the hash_update_stream function, which allows us to read the file in chunks and compute the hash incrementally, effectively reducing memory usage.

1. Introduction to the hash_update_stream Function

hash_update_stream is a PHP function used to update hash calculations. It accepts an open file stream (stream) and updates the hash of the file content chunk by chunk. Compared to loading the entire file content into memory directly, hash_update_stream can process large files step by step, significantly reducing memory usage.

The function signature is as follows:

bool hash_update_stream ( resource $context , resource $file_handle [, int $length = 1024 ] )
  • $context: This is a hash context created by the hash_init function.

  • $file_handle: The resource handle of the file, usually obtained via the fopen function.

  • $length: The number of bytes to read each time, defaulting to 1024 bytes (1KB).

2. Solving Memory Issues When Handling Large Files

Suppose we have a large file and need to calculate its MD5 hash value. The traditional method is to read the entire file into memory and then compute the hash. However, for large files, this approach may cause memory overflow.

$file = 'path/to/largefile.txt'; // Replace with the actual file path
$context = hash_init('md5');
<p>$handle = fopen($file, 'rb');<br>
if ($handle) {<br>
while (!feof($handle)) {<br>
$buffer = fread($handle, 1024); // Read 1KB of data<br>
hash_update($context, $buffer);<br>
}<br>
fclose($handle);<br>
}</p>
<p>$hash = hash_final($context);<br>
echo "File MD5 hash: " . $hash;<br>

As shown above, we read the file in chunks and use hash_update to update the hash value. Here, hash_update calculates the hash on smaller blocks, but memory usage still depends on the block size.

3. Using hash_update_stream to Optimize Memory Usage

Unlike directly using hash_update, the hash_update_stream function allows us to process the file stream directly without manually reading file content. This makes it more convenient to update the hash incrementally while reducing memory consumption.

The optimized code is as follows:

$file = 'path/to/largefile.txt'; // Replace with the actual file path
$context = hash_init('md5');
<p>$handle = fopen($file, 'rb');<br>
if ($handle) {<br>
while (!feof($handle)) {<br>
// Read 1024 bytes from the file stream and update the hash each time<br>
hash_update_stream($context, $handle, 1024);<br>
}<br>
fclose($handle);<br>
}</p>
<p>$hash = hash_final($context);<br>
echo "File MD5 hash: " . $hash;<br>

4. Why is hash_update_stream More Efficient?

hash_update_stream has the advantage of handling file streams directly without the need to manually read each chunk. This approach effectively reduces memory usage when computing hash values.

  1. Chunked Reading: hash_update_stream reads data from the file in chunks rather than loading the entire file into memory at once.

  2. Low Memory Consumption: Through streaming operations, memory usage is minimal, only storing the current block being processed.

  3. Suitable for Large Files: For very large files (several GB or more), hash_update_stream is the ideal choice.

5. URL Replacement in Practical Applications

In real-world applications, you may need to calculate the hash value of a remote file. Suppose you need to calculate the hash of a file pointed to by a URL, you can use a similar approach:

$url = 'http://example.com/largefile.txt'; // Example URL
$file = fopen($url, 'rb');
if ($file) {
    $context = hash_init('md5');
    while (!feof($file)) {
        hash_update_stream($context, $file, 1024);
    }
    fclose($file);
echo "File MD5 hash: " . $hash;

}

Since you requested to replace the URL domain with m66.net, we can modify the above URL as follows: