PHP sha1_file() Function

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

ParameterIs RequiredDescription
fileRequired.The file to be calculated
rawOptional.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/*  w w  w. ja  v a  2  s  .  c  om*/
$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// w  w w . j a  v a2  s . 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.";
}
?>




















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions