PHP 字符串函数

PHP sha1_file() 函数它用于计算文件的 SHA-1 哈希值。它使用美国Secure Hash 算法1 。

它返回计算出的 SHA-1 哈希值,默认是 40 个字符的十六进制数。

语法

sha1_file(file,raw);

参数

参数描述必须/可选
file指定要计算的文件。必须
raw指定十六进制或二进制输出格式:
  • TRUE:20 字符二进制格式。
  • FALSE:默认值。 40 个字符的十六进制数字。
可选

示例

介绍一些例子,了解PHP sha1_file() 函数的使用方法。

示例1

保存: "test.txt",内容为

Hello yxjc123

保存:test.php

<?php
$filename = "test.txt";
echo "Your filename is:". $filename;
echo "<br>";
echo "By using sha1_file() function: ".sha1_file($filename);
?>

输出:

Your filename is:test.txt
By using sha1_file() function: f9bfa0c75c519fbc2df2d43be641fe956dd3065a

示例2

校验文件是否已更改。

保存:"test.txt"内容为

Hello yxjc123

保存:"sha1file.txt"内容为

f9bfa0c75c519fbc2df2d43be641fe956dd3065a

保存:"index.php"代码为

<?php
$sha1file = sha1_file("test.txt");
file_put_contents("sha1file.txt",$sha1file);
?>

保存:"test.php"的代码为

<?php
$sha1file = file_get_contents("sha1file.txt");
if (sha1_file("test.txt") == $sha1file){
  echo "The file is ok.";
}
else{
  echo "The file has been changed.";
}
?>

输出:

The file is ok.