Latest web development tutorials

PHP md5_file () function

PHP String Reference PHP String Reference

Examples

Calculation text file "test.txt" MD5 hash:

<?php
$filename = "test.txt";
$md5file = md5_file($filename);
echo $md5file;
?>

The code above will output:

d41d8cd98f00b204e9800998ecf8427e


Definition and Usage

md5_file () MD5 hash function calculates file.

md5_file () function uses the RSA Data Security, including the MD5 message digest algorithm.

RFC 1321 interpretation from - MD5 message digestalgorithm:MD5message digest algorithm of arbitrary length message as input, and converts it into a 128-bit length of the "fingerprint" or "message digest" value to represent the enter a value, and the converted value as a result.MD5 algorithm is mainly for digital signature applications designed; in this digital signature applications, large files will be encrypted (encryption process here is through a password system under: public-key [such as RSA] under setting the private key and completed) in a secure manner prior to compression.

To calculate the MD5 hash of a string, use the md5 () function.


grammar

md5_file( file,raw )

参数 描述
file 必需。规定要计算的文件。
raw 可选。一个规定十六进制或二进制输出格式的布尔值:
  • TRUE - 原始 16 字符二进制格式
  • FALSE - 默认。32 字符十六进制数

technical details

return value: If successful calculated MD5 hash, if it fails it returns FALSE.
PHP version: 4.2.0+
Update log: In PHP 5.0 in, raw parameter becomes optional.

Since PHP 5.1 onwards, you can use the package md5_file (). For example: md5_file ( "http://w3cschool.cc/ ..")


More examples

Example 1

Stored in the file "test.txt" MD5 hash:

<?php
$md5file = md5_file("test.txt");
file_put_contents("md5file.txt",$md5file);
?>

Detecting whether "test.txt" has been changed (that is, 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 code above will output:

The file is ok.



PHP String Reference PHP String Reference