Current Location: Home> Latest Articles> How to Implement Chunked Hash Upload with the hash_update_stream Function

How to Implement Chunked Hash Upload with the hash_update_stream Function

M66 2025-07-04

When handling large file uploads, chunked upload is a commonly used technique. It allows a file to be divided into smaller chunks and uploaded one by one without waiting for the entire file to finish uploading. Meanwhile, to ensure the file has not been tampered with during the transfer process, hash algorithms are often used to verify the integrity of the file. PHP provides the hash_update_stream function, which can help implement chunked hash uploads. This article will explain in detail how to use the hash_update_stream function for this purpose.

1. Introduction to the hash_update_stream Function

hash_update_stream is a hash function in PHP used to perform hash calculations on a stream (such as a file or data stream). In the context of chunked uploads, the function can be called each time a file chunk is uploaded to update the hash value, ensuring that each file chunk is correctly hashed and verified for integrity.

Function prototype:

bool hash_update_stream ( resource $context , resource $handle [, int $length = 8192 ] )
  • $context: Specifies a hash context, usually created with the hash_init function.

  • $handle: A resource pointing to the opened file stream or data stream.

  • $length: The number of bytes to read, with a default value of 8192 bytes.

2. How to Implement Chunked Hash Upload

Suppose we have a large file that needs to be uploaded in chunks, and each uploaded chunk must be hashed for verification. We can follow the steps below to implement chunked hash upload:

2.1 Create Hash Context

First, create a hash context and choose a hash algorithm (such as sha256) for the calculation.

$hashContext = hash_init('sha256');

2.2 Open the File and Upload in Chunks

Next, we open the file stream and upload the file's content in chunks. Each time a chunk is uploaded, we call the hash_update_stream function to update the hash value.

$filePath = 'path/to/largefile.zip'; // File path
$chunkSize = 8192; // Size of each chunk
<p>$fileHandle = fopen($filePath, 'rb'); // Open file stream<br>
if (!$fileHandle) {<br>
die('Unable to open the file');<br>
}</p>
<p>while (!feof($fileHandle)) {<br>
// Read a chunk from the file stream<br>
$chunk = fread($fileHandle, $chunkSize);</p>
hash_update_stream($hashContext, $chunk);

// Upload the chunk (assuming a function to upload each chunk)
// upload_chunk($chunk);

}

// Close the file stream
fclose($fileHandle);

2.3 Compute and Return the Final Hash Value

After all chunks have been uploaded and processed, we can use the hash_final function to get the final hash value.

$finalHash = hash_final($hashContext);
echo "The hash value of the file is: $finalHash";

In this way, we ensure that each file chunk is properly hashed, and we ultimately obtain the hash value for the entire file.

3. Verifying the Chunked Upload Hash with the Server

During chunked upload, after each file chunk is uploaded, the client can send the corresponding hash value (or the entire file’s hash) to the server. The server then verifies the integrity of the file by comparing the received hash value.

The server-side verification process is as follows:

3.1 Receive and Verify the Hash

// Assume the hash value uploaded by the client is received
$receivedHash = $_POST['file_hash'];
<p>// The server recalculates the hash<br>
$serverHashContext = hash_init('sha256');<br>
$fileHandle = fopen('path/to/uploaded_chunk', 'rb'); // Open the uploaded chunk</p>
<p>while (!feof($fileHandle)) {<br>
$chunk = fread($fileHandle, 8192);<br>
hash_update_stream($serverHashContext, $chunk);<br>
}</p>
<p>$calculatedHash = hash_final($serverHashContext);</p>
<p>// Verify the hash<br>
if ($calculatedHash === $receivedHash) {<br>
echo 'File chunk hash verification passed';<br>
} else {<br>
echo 'File chunk hash verification failed';<br>
}</p>
<p>fclose($fileHandle);<br>

4. Conclusion

Using the hash_update_stream function to implement chunked hash uploads is a simple and effective method to ensure the integrity of each file chunk during the upload process. By following the steps outlined above, we can upload a file in chunks and use hash verification to ensure that the data has not been tampered with. It is important to ensure that each chunk is uploaded in the correct order, and the server should also perform the corresponding hash verification.

This concludes the basic method of implementing chunked hash upload with the hash_update_stream function. I hope this article has been helpful!