PHP sha1_file() Function
In this chapter you will learn:
- Definition for PHP sha1_file() Function
- Syntax for PHP sha1_file() Function
- Parameter for PHP sha1_file() Function
- Return for PHP sha1_file() Function
- Example - Calculate the SHA-1 hash of the text file "test.txt"
- Example - Check the sha1 value
Definition
The sha1_file() function calculates the SHA-1 hash of a file.
Syntax
PHP sha1_file() Function has the following syntax.
sha1_file(file,raw)
Parameter
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.
- TRUE - Raw 20 character binary format
- FALSE - Default. 40 character hex number
Return
PHP sha1_file() Function Returns the calculated SHA-1 hash on success, or FALSE on failure
Example 1
Calculate the SHA-1 hash of the text file "test.txt":
<?php/*j a va 2s. c o m*/
$filename = "test.txt";
$sha1file = sha1_file($filename);
echo $sha1file;
?>
Example 2
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 j a v a 2 s. co m*/
$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.";
}
?>
Next chapter...
What you will learn in the next chapter:
- Definition for PHP stat() Function
- Syntax for PHP stat() Function
- Parameter for PHP stat() Function
- Return for PHP stat() Function
- Note on PHP stat() Function
- Example - returns information about a file.
Home » PHP Tutorial » PHP File Functions