函數(shù)名:hash_file()
適用版本:PHP 4 >= 4.3.0, PHP 5, PHP 7
函數(shù)描述:hash_file() 函數(shù)計(jì)算文件的哈希值。
用法:
string hash_file ( string $algo , string $filename [, bool $raw_output = FALSE ] )
參數(shù):
$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
:要計(jì)算哈希值的文件路徑。$raw_output
(可選):如果設(shè)置為TRUE,則返回原始二進(jìn)制數(shù)據(jù)。默認(rèn)為FALSE,返回小寫的十六進(jìn)制字符串。
返回值: 返回計(jì)算出的哈希值,或在失敗時(shí)返回FALSE。
示例:
$file = 'path/to/file.txt';
$hash = hash_file('md5', $file);
echo $hash;
上述示例將計(jì)算指定文件的MD5哈希值,并將結(jié)果打印輸出。
注意:在使用hash_file()函數(shù)之前,需要確保所選的哈希算法在當(dāng)前PHP環(huán)境中可用??梢酝ㄟ^調(diào)用hash_algos()函數(shù)來獲取當(dāng)前環(huán)境支持的所有哈希算法列表。