hash_file
(PHP 5 >= 5.1.2, PHP 7, PHP 8, PECL hash >= 1.1)
hash_file — 给指定文件的内容生成哈希值
说明
hash_file
( string
$algo
, string $filename
, bool $binary
= false
) : string|false参数
-
algo
-
要使用的哈希算法的名称(例如:"md5","sha256","haval160,4" 等)。 可以在 hash_algos() 中查看当前支持的算法。
-
filename
-
要进行哈希运算的文件位置的 URL ;支持 fopen 封装协议。
-
binary
-
设置为
true
时,输出原始二进制数据。 设置为false
时,输出小写的十六进制字符串。
返回值
如果 binary
设置为 true, 则返回原始二进制数据表示的信息摘要,
否则返回十六进制小写字符串格式表示的信息摘要。
范例
示例 #1 使用 hash_file()
<?php
/* 创建一个要计算哈希值的文件 */
file_put_contents('example.txt', 'The quick brown fox jumped over the lazy dog.');
echo hash_file('md5', 'example.txt');
?>
以上例程会输出:
5c6ffbdd40d9556b73a21e63c3e0e904
参见
- hash() - 生成哈希值 (消息摘要)
- hash_hmac_file() - 使用 HMAC 方法和给定文件的内容生成带密钥的哈希值
- hash_update_file() - 从文件向活跃的哈希运算上下文中填充数据
- md5_file() - 计算指定文件的 MD5 散列值
- sha1_file() - 计算文件的 sha1 散列值