Compute the MD5 hash of the text file "test.txt":
<?php$filename = "test.txt";$md5file = md5_file($filename);echo $md5file;?>The above code will output:
d41d8cd98f00b204e9800998ecf8427eThe md5_file() function calculates the MD5 hash of a file.
The md5_file() function uses RSA data security, including the MD5 message digest algorithm.
Explanation from RFC 1321 - MD5 message digest algorithm: The MD5 message digest algorithm takes information of any length as an input value and converts it into a 128-bit length "fingerprint information" or "message digest" value to represent this Enter a value and get the converted value as the result. The MD5 algorithm is primarily designed for digital signature applications where larger files are encrypted using a public key in a cryptographic system such as RSA. (done by setting a private key) before compressing in a secure manner.
To calculate the MD5 hash of a string, use the md5() function.
md5_file( file,raw )
parameter | describe |
---|---|
file | Required. Specifies the file to be calculated. |
raw | Optional. A Boolean value specifying the hexadecimal or binary output format: TRUE - raw 16-character binary format FALSE - Default. 32-character hexadecimal number |
Return value: | Returns the calculated MD5 hash on success, or FALSE on failure. |
---|---|
PHP version: | 4.2.0+ |
Update log: | In PHP 5.0, the raw parameter becomes optional. Since PHP 5.1, md5_file() can be used through wrappers. For example: md5_file("http://w3cschool.cc/..") |
Store the MD5 hash of "test.txt" in the file:
<?php$md5file = md5_file("test.txt");file_put_contents("md5file.txt",$md5file);?>Detect whether "test.txt" has been changed (i.e. whether the MD5 hash has been changed):
<?php$md5file = file_get_contents("md5file.txt");if (md5_file("test.txt") == $md5file) { echo "The file is ok."; }else { echo "The file has been changed ."; }?>The above code will output:
The file is ok.