Home >>PHP String Functions >PHP md5_file() Function
PHP md5_file() function is used to generate the md5 hash value of a given input file. On success of the operation this function returns a hash string otherwise it returns FALSE. It accepts two parameters out of which one is required, which is the given input file and other one is an optional parameter.
Syntax:-
md5_file( $file, $raw );
Parameter | Description |
---|---|
String | This is a required parameter. This parameter contains the file which is to be converted into hash. |
RawOutput | This is an optional parameter. This parameter specifies the hex or binary output format. It is of Boolean type.
|
Here is an example of md5_file() function in PHP:
<html> <body> <?php $file = "file.txt"; $md5file = md5_file($file); echo $md5file; ?> </body> </html>
Here is an example of using md5_file() function in PHP for file verification:
First we will store the md5 hash into the file.
<html> <body> <?php $file = "file.txt"; $md5file = md5_file($file); file_put_contents("md5file.txt",$md5file); // storing the md5 hash file ?> </body> </html>
Then get the md5 file & match it with the hash value.
<html> <body> <?php $md5file = file_get_contents("md5file.txt"); if (md5_file("file.txt") == $md5file) { echo "The file is OK."; } else { echo "The file has been altered."; } ?> </body> </html>