In web development, it is very important to verify the integrity of files and prevent file tampering. PHP provides some powerful file manipulation functions that can be used to ensure the security and integrity of uploaded files. The hash_update_stream function is a useful tool for real-time update of hash values. It is suitable for calculating the hash value of a file step by step when processing large files (such as pictures) without loading the entire file into memory.
This article will introduce how to use the hash_update_stream function to perform real-time hash verification of uploaded images and ensure that the file content has not been modified.
During the file upload process, in order to ensure that the file has not been tampered with, we can use a hash algorithm (such as SHA-256 or MD5) to calculate the hash value of the file. By comparing the hash value of the uploaded file with the expected hash value, we can determine whether the file has been tampered with during the transmission process.
The hash_update_stream function is an advanced function in PHP that allows us to update the hash value in real time when streaming the file. This is very useful for handling large files, as it avoids loading the entire file into memory.
Suppose we have an HTML form that allows users to upload images. We will use PHP to process uploading files and calculate their hash. We will stream the content of the file and update the hash value of the file in real time during the upload process.
Here is a simple PHP code example showing how to use the hash_update_stream function to perform real-time hash verification of uploaded images.
<?php
// Set Upload Directory
$uploadDir = 'uploads/';
// Check if there are files uploads
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
// Make sure the uploaded file is an image
$allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif'];
if (in_array($file['type'], $allowedMimeTypes)) {
// Get temporary path to the file
$tempFile = $file['tmp_name'];
// Open the file stream
$fileHandle = fopen($tempFile, 'rb');
// Select a hashing algorithm
$hashContext = hash_init('sha256');
// usehash_update_streamStreaming hashing to the file
while ($chunk = fread($fileHandle, 8192)) {
hash_update_stream($hashContext, $chunk);
}
// Get the final hash value
$fileHash = hash_final($hashContext);
// Print the hash value of the uploaded file
echo "Uploaded file hash: " . $fileHash . "<br>";
// Close the file stream
fclose($fileHandle);
// Save the file to the target directory
move_uploaded_file($tempFile, $uploadDir . $file['name']);
// Suppose we precalculate the hash value of the file
$expectedHash = 'Pre-calculated hash value';
// Compare hash values
if ($fileHash === $expectedHash) {
echo "File hash verification successfully!";
} else {
echo "File hash verification failed!";
}
} else {
echo "Only image file uploads are supported!";
}
}
?>
<!-- Upload form -->
<form method="POST" enctype="multipart/form-data">
<label for="image">Upload pictures:</label>
<input type="file" name="image" id="image">
<button type="submit">Upload</button>
</form>
File upload form : First, we created an HTML form through which users can upload image files.
Check the uploaded file type : When processing files, first check whether the uploaded file type is the allowed image format (such as JPEG, PNG, GIF).
Open file flow : Open a temporary file that uploads files in binary mode through the fopen function.
Initialize hash algorithm : Use the hash_init function to select a hash algorithm (such as SHA-256).
Real-time update hash value : Through the hash_update_stream function, read the file contents block by block and update the hash value in real time. The fread function reads part of the data in the file and reads 8KB of contents each time.
Calculate hash value : After all the file contents are read, use the hash_final function to obtain the final hash value.
File Save : If hash verification is passed, move the file to the specified upload directory.
Hash verification : We verify whether the file has been tampered with by comparing the calculated hash value with the expected hash value.
The hash_update_stream function can avoid memory overflow problems when processing large files. It reads the file as a stream, reads a small piece of data at a time (8KB in this case), and updates the hash value for each piece of data. Streaming can significantly reduce memory usage, especially for large files, compared to methods of calculating hash values by loading the entire file into memory.
Using the hash_update_stream function for file hash verification is an efficient and safe way, especially when dealing with large files. By calculating the hash value of the file in real time, the integrity and security of the file content can be ensured during the file upload process and prevented files from being tampered with.