This article describes how to obtain the MD5 value of a file in PHP and determine whether the file has been modified. MessageDigestAlgorithmMD5 (the fifth version of the message digest algorithm in Chinese) it is a hash function widely used in the computer security field to provide message integrity protection. if you need it, you can check whether the uploaded file has been modified, you need to record the md5 value of the uploaded file. here we record the method for obtaining the md5 value of the file.
The code is as follows:
If (isset ($ _ FILES ['multimedia ']) & $ _ FILES ['multimedia'] ['error'] = 0)
{
$ File_name = $ _ FILES ['multimedia '] ['name'];
$ Size = getimagesize ($ _ FILES ['multimedia '] ['tmp _ name']);
$ Type = $ _ FILES ['multimedia '] ['type'];
$ Original = $ _ FILES ['multimedia '] ['tmp _ name'];
$ Md5 = md5_file ($ original );
Echo $ md5;
}
Md5_file ()
MD5 hash of the md5_file () function compute file. The md5 () function uses RSA Data Security, including the MD5 packet excerpt algorithm. If the calculation succeeds, the calculated MD5 hash is returned. if the calculation fails, false is returned.
Syntax: md5 (string, raw)
The string parameter, required. Specifies the file to be calculated.
The charlist parameter. optional. The hexadecimal or binary output format is required: TRUE-original 16-character binary format; FALSE-default. 32 characters in hexadecimal notation.
The code is as follows:
<? Php
$ Filename = "test.txt ";
$ Md5file = md5_file ($ filename );
Echo $ md5file;
?>
MD5 hash for storing the "test.txt" file:
The code is as follows:
<? Php
$ Md5file = md5_file ("test.txt ");
File_put_contents ("md5file.txt", $ md5file );
?>
In this example, we will check whether "test.txt" has been changed (that is, whether the MD5 hash has been changed ):
The code is as follows:
<? 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 .";
}
?>
Output:
The code is as follows:
The file is OK.