Home >>PHP String Functions >PHP sha1_file() Function
PHP sha1_file function is used to calculate the sha1 hash of a given file. This function generates the SHA1 hash of the text file. This function uses the US Secure Hash Algorithm 1. In the case of success it returns the output string and in the case of failure it returns FALSE.
Syntax:
sha1_file($file, $raw);
Parameter | Description |
---|---|
string | This is a required parameter. This parameter contains the file to be calculated. |
raw | This is an optional parameter. This parameter defines hex or binary output format:
|
Here is an example of sha1-file() function in PHP:
<html> <body> <?php $filename = "somefile.txt"; $sha1file = sha1_file($filename); echo $sha1file; ?> </body> </html>
Here is an another example of sha1_file() function in PHP:
Store the file first.
<html> <body> <?php $sha1file = sha1_file("jerry.txt"); file_put_contents("sha1file.txt",$sha1file); ?> </body> </html>
Now Check the file.
<html> <body> <?php $sha1file = file_get_contents("sha1file.txt"); if (sha1_file("jerry.txt") == $sha1file) { echo "The file is OK."; } else { echo "The file has been damaged !!"; } ?> </body> </html>