The sha1_file() function calculates the SHA-1 hash of a file.
PHP sha1_file() Function has the following syntax.
sha1_file(file,raw)
Parameter | Is Required | Description |
---|---|---|
file | Required. | The file to be calculated |
raw | Optional. | A boolean value that specifies hex or binary output format: |
Possible values for raw.
PHP sha1_file() Function Returns the calculated SHA-1 hash on success, or FALSE on failure
Calculate the SHA-1 hash of the text file "test.txt":
<?php
$filename = "test.txt";
$sha1file = sha1_file($filename);
echo $sha1file;
?>
Store the SHA-1 hash of "test.txt" in a file and test if "test.txt" has been changed (that is if the SHA-1 hash has been changed):
<?php//from w w w .ja va 2s . c om
$sha1file = sha1_file("test.txt");
file_put_contents("sha1file.txt",$sha1file);
$sha1file = file_get_contents("sha1file.txt");
if (sha1_file("test.txt") == $sha1file){
echo "The file is ok.";
}else{
echo "The file has been changed.";
}
?>