What is PHP md5_file() Function?
Hash or hashing is a mathematical process that converts a variable size data to a fixed length string. The md5_file() function calculates a md5 hash of a file.
How does PHP md5_file() Function Works?
The function uses RSA Data Security, Inc. MD5 Message-Digest Algorithm. Conceptually, the function follows the following steps to compute the hash-
- The function reads the content of the file.
- It converts the input string to bytes.
- Then, it broken into 512-bit blocks
- The function uses four 32-bit registers, labeled A, B, C, and D, to hold the intermediate and final hash value.
- Each block goes through 64 rounds of operations (XOR, AND, OR, and NOT).
- Finally, concatenating all four registers to form the final hash.
Syntax:
md5_file(filename, binary)
Parameters:
The Function has 1 required parameter and 1 optional parameter-
filename (Required): It specifies the path to the file.
binary (Optional): It specifies a Boolean value–
- TRUE – If it is TRUE, the function returns a 16 byte raw binary format of the md5 hash.
- FALSE (default) – If it is FALSE, the function returns a 32 character long hexadecimal number of the md5 hash.
Return Values:
The function returns-
- md5 hash of a string on success or
- FALSE on failure.
Examples:
Example 1:
<?php
$hash = md5_file("file2.txt");
echo "MD5 hash is: $hash and its length is: " . strlen($hash);
?>
Output:
MD5 hash is: e10adc3949ba59abbe56e057f20f883e and its length is: 32
Example 2: Running md5_file of an empty file-
<?php
$hash = md5_file("file2.txt");
echo "MD5 hash of an empty file is: $hash and still its length is: " . strlen($hash);
?>
Output:
MD5 hash of an empty file is: e10adc3949ba59abbe56e057f20f883e and still its length is: 32
Example 3:
<?php
$hash = md5_file("file2.txt", TRUE);
echo "Raw binary format of the MD5 hash of a file is: $hash and its length is: " . strlen($hash);
?>
Output:
Raw binary format of the MD5 hash of a file is: � �9I�Y��V�W��> and its length is: 16
Example 4: Comparing a string and its hash-
<?php
$string = "123456";
$savedHash = "e10adc3949ba59abbe56e057f20f883e"; // md5 hash of "123456".
if (md5_file("file2.txt") === $savedHash){
echo "Hash matched. The file & the hash are same.";
} else {
echo "Hash not matched. The file & the hash are not same.";
}
?>
Output:
Hash matched. The file & the hash are same.
Note on md5_file() Function:
- This function works same as md5() file. Instead of hashing a string, the md5_file() function hashes the entire content of a file.
- The function does not hash the filename, instead, it hashes the content inside the file.
- It is a one-way operation. You cannot “decrypt” a SHA-1 hash back into the original string.
Practical Usages of md5_file() Function:
- This function is used to check integrity of a file (to know whether the file has been changed or not).
- It can also be used to verify data. But, keep in mind that it doesn’t ensure security of the data.
- This function can also be used for generating unique identifiers.
Caution:
This function is not recommended to use for password security as from two different inputs, the function can create same hash). To generate secure password, use the these function instead – password_hash() and password_verify()..These function use Bcrypt or Argon2 which are much more secure.
PHP Version Support:
PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8
Summary: PHP md5_file() Function
PHP md5_file() function is one of the built-I string function. It is a fast and insecure function. Use this function to create 32-character long hexadecimal of a string.