函数名:hash_file()
适用版本:PHP 4 >= 4.3.0, PHP 5, PHP 7
函数描述:hash_file() 函数计算文件的哈希值。
用法:
string hash_file ( string $algo , string $filename [, bool $raw_output = FALSE ] )
参数:
$algo
:要使用的哈希算法的名称。可以使用的算法有:"md2"、"md4"、"md5"、"sha1"、"sha224"、"sha256"、"sha384"、"sha512"、"ripemd128"、"ripemd160"、"ripemd256"、"ripemd320"、"whirlpool"、"tiger128,3"、"tiger160,3"、"tiger192,3"、"tiger128,4"、"tiger160,4"、"tiger192,4"、"snefru"、"gost"、"crc32"、"crc32b"、"adler32"。$filename
:要计算哈希值的文件路径。$raw_output
(可选):如果设置为TRUE,则返回原始二进制数据。默认为FALSE,返回小写的十六进制字符串。
返回值: 返回计算出的哈希值,或在失败时返回FALSE。
示例:
$file = 'path/to/file.txt';
$hash = hash_file('md5', $file);
echo $hash;
上述示例将计算指定文件的MD5哈希值,并将结果打印输出。
注意:在使用hash_file()函数之前,需要确保所选的哈希算法在当前PHP环境中可用。可以通过调用hash_algos()函数来获取当前环境支持的所有哈希算法列表。