Current Location: Home> Latest Articles> Detailed explanation of the basic usage of md5_file() function

Detailed explanation of the basic usage of md5_file() function

M66 2025-06-02

File integrity verification is a very common requirement in PHP development, especially when handling uploads, cached files, or secure verification. The md5_file() function is used to calculate the MD5 hash value of a file and is a simple and practical tool. This article will introduce in detail the usage of md5_file() function and explain its application in actual scenarios through examples.


What is the md5_file() function?

md5_file() is a built-in PHP function that calculates the MD5 hash value of a specified file. MD5 (Message-Digest Algorithm 5) is a widely used hash function that generates a 32-bit hexadecimal string to uniquely identify file contents.

Function definition:

 string md5_file ( string $filename [, bool $raw_output = false ] )
  • $filename : Required, file path.

  • $raw_output : optional, whether to output in original binary format. Default false , returns a 32-bit hexadecimal string.

The function returns the MD5 value of the file content, and returns false if it fails.


Basic usage of md5_file()

Here is a simplest example to calculate the MD5 value of a file:

 <?php
$file = 'example.txt';
$md5 = md5_file($file);

if ($md5 !== false) {
    echo "document $file ofMD5The value is:$md5";
} else {
    echo "无法读取document $file";
}
?>

In this example, we pass in the file path example.txt and print out the MD5 value of the file. If the file does not exist or cannot be read, an error message is returned.


Application of md5_file() in real scenarios

1. File integrity verification

When uploading a file, the client can first calculate the MD5 value of the file, and after uploading, the server will also calculate the MD5 to confirm that the two are consistent and avoid errors or tampering during file transfer.

 <?php
// 假设客户端上传了document,服务器端接收到document路径$uploadedFile

$serverMd5 = md5_file($uploadedFile);
$clientMd5 = $_POST['file_md5']; // Assume that the client has submittedmd5value

if ($serverMd5 === $clientMd5) {
    echo "document验证成功,Complete and correct。";
} else {
    echo "document验证失败,document可能被篡改。";
}
?>

2. Cache file change detection

In the cache system, the MD5 value of the file can be used to determine whether the file has been modified and whether to update the cache.

 <?php
$cacheFile = '/path/to/cache/data.cache';
$currentMd5 = md5_file($cacheFile);

if ($currentMd5 !== $storedMd5) {
    // document内容发生变化,Regenerate cache
    regenerateCache();
    $storedMd5 = $currentMd5;
}
?>

3. Security verification of download files

The file hash is calculated by md5_file() and the download link can be used to prevent illegal downloads or links from being tampered with.

 <?php
$file = 'files/sample.zip';
$expectedMd5 = md5_file($file);

echo "点击下载document:<a href='https://m66.net/download.php?file=sample.zip&md5=$expectedMd5'>Download link</a>";
?>

Things to note

  • md5_file() reads the entire file content for calculation, which will have an impact on the performance of large files. It is recommended to use it reasonably.

  • The MD5 algorithm has been proven to have collision risks. It is recommended to use a safer hash algorithm (such as hash_file('sha256', $filename) ) in security-sensitive scenarios.

  • The incoming file path must be readable, otherwise false will be returned.


Through this article, you have mastered the basic usage methods of the md5_file() function and several typical application scenarios. In actual development, combined with file verification requirements, md5_file() is a very convenient tool.