当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > md5_file() 函数
The md5_file() function calculates the MD5 hash of a file.
md5_file()函数的作用是:计算文件中的MD5 hash。
The md5_file() function uses the RSA Data Security, Inc. MD5 Message-Digest Algorithm.
md5()函数使用美国实验室(以研究加密算法而著名)数据安全加密。它采用MD5信息散列[Message-Digest]运算法则
From RFC 1321 - The MD5 Message-Digest Algorithm: "The MD5 message-digest algorithm takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input. The MD5 algorithm is intended for digital signature applications, where a large file must be "compressed" in a secure manner before being encrypted with a private (secret) key under a public-key cryptosystem such as RSA."
RFC1321的解释 - MD5信息散列[Message-Digest]运算法则:“MD5信息散列运算法则将任意长度的信息作为输入值,并将其换算成一个128位长度的“指纹信息”或“信息散列”值来代表这个输入值,并以换算后的值作为结果。MD5运算法则主要是为“数字签名程序”而设计的;在这个“数字签名程序“中,较大的文件将在加密(这里的加密过程是通过在一个密码系统下[如:RSA]的公开密匙下设置私要密匙而完成的)之前以一种安全的方式进行压缩。”
This function returns the calculated MD5 hash on success, or FALSE on failure.
如果函数执行成功将计算MD5 hash,如果失败返回false。
md5_file(file,raw) |
Parameter参数 | Description描述 |
---|---|
file | Required. The file to be calculated 必要参数。指定需要计算的字符串 |
raw | Optional. Specifies hex or binary output format: 可选参数。指定输出结果的格式(包括十六进制和二进制)
Note: This parameter was added in PHP 5.0 |
<?php $filename = "test.txt"; $md5file = md5_file($filename); echo $md5file; ?> |
The output of the code above will be:
上述代码将输出下面的结果:
5d41402abc4b2a76b9719d911017c592 |
Store the MD5 hash of "test.txt" in a file:
将“test.txt”文件中的MD5 hash存储进一个文件中:
<?php $md5file = md5_file("test.txt"); file_put_contents("md5file.txt",$md5file); ?> |
In this example we will test if "test.txt" has been changed (that is if the MD5 hash has been changed):
在下面的案例中,我们将测试“test.txt”文件是否已经改变:
<?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 output of the code above could be:
上述代码将输出下面的结果:
The file is ok. |