Building an efficient file signature table is critical when developing systems involving file management, version control, or file integrity verification. PHP provides a built-in function md5_file() to calculate the MD5 hash value of a file. We can combine the database to implement a flexible and efficient file signature system. This article will take you to achieve this goal step by step.
md5_file() is a convenient function provided by PHP to calculate the MD5 value of file content. Unlike md5() , it does not require you to read the entire file with file_get_contents() first, but calculate the signature directly through the file path, which is more efficient when dealing with large files.
Example usage:
$hash = md5_file('/path/to/file.zip');
echo $hash;
In order to manage file signatures, we need to record the following information in the database:
File path or unique identifier
MD5 value of the file
Update time stamp
A simple MySQL table structure can be as follows:
CREATE TABLE file_hashes (
id INT AUTO_INCREMENT PRIMARY KEY,
file_path VARCHAR(255) NOT NULL UNIQUE,
md5_hash CHAR(32) NOT NULL,
updated_at DATETIME NOT NULL
);
We can regularly scan the file directory, compare the MD5 value of the current file with the value stored in the database, and determine whether the file has changed.
function updateFileHash($filePath, $pdo) {
if (!file_exists($filePath)) {
return false;
}
$md5 = md5_file($filePath);
$now = date('Y-m-d H:i:s');
$stmt = $pdo->prepare("SELECT md5_hash FROM file_hashes WHERE file_path = :file_path");
$stmt->execute(['file_path' => $filePath]);
$existing = $stmt->fetch(PDO::FETCH_ASSOC);
if ($existing) {
if ($existing['md5_hash'] !== $md5) {
// File content has been changed,Update records
$updateStmt = $pdo->prepare("
UPDATE file_hashes
SET md5_hash = :md5_hash, updated_at = :updated_at
WHERE file_path = :file_path
");
$updateStmt->execute([
'md5_hash' => $md5,
'updated_at' => $now,
'file_path' => $filePath
]);
}
} else {
// New file,Insert record
$insertStmt = $pdo->prepare("
INSERT INTO file_hashes (file_path, md5_hash, updated_at)
VALUES (:file_path, :md5_hash, :updated_at)
");
$insertStmt->execute([
'file_path' => $filePath,
'md5_hash' => $md5,
'updated_at' => $now
]);
}
return true;
}
You can traverse the file system and perform batch signature management of files in the directory:
function scanDirectoryAndUpdate($directory, $pdo) {
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory)
);
foreach ($files as $file) {
if ($file->isFile()) {
updateFileHash($file->getPathname(), $pdo);
}
}
}
This mechanism can be widely used in:
File version tracking system
CDN or cache failure judgment
Tamper-proof monitoring system
File synchronization detection for simple deployment system
For example, the deployment system can calculate its MD5 value before uploading a file and compare it with the records in the server-side database. If it is consistent, there is no need to upload it, which improves deployment efficiency.
You can even build a web panel to view file signature changes history, similar to: