PHP sha1_file() Function

In this chapter you will learn:

  1. Definition for PHP sha1_file() Function
  2. Syntax for PHP sha1_file() Function
  3. Parameter for PHP sha1_file() Function
  4. Return for PHP sha1_file() Function
  5. Example - Calculate the SHA-1 hash of the text file "test.txt"
  6. 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

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/*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:

  1. Definition for PHP stat() Function
  2. Syntax for PHP stat() Function
  3. Parameter for PHP stat() Function
  4. Return for PHP stat() Function
  5. Note on PHP stat() Function
  6. Example - returns information about a file.
Home » PHP Tutorial » PHP File Functions
PHP basename() function
PHP chdir() function
PHP chgrp() Function
PHP chmod() Function
PHP chown() Function
PHP chroot() Function
PHP clearstatcache() Function
PHP closedir() Function
PHP copy() Function
PHP delete() function
PHP dir() Function
PHP dirname() Function
PHP disk_free_space() Function
PHP disk_total_space() Function
PHP diskfreespace() Function
PHP fclose() Function
PHP feof() Function
PHP fflush() Function
PHP fgetc() Function
PHP fgets() Function
PHP fgetss() Function
PHP file() Function
PHP file_exists() Function
PHP file_get_contents() Function
PHP file_put_contents() Function
PHP fileatime() Function
PHP filectime() Function
PHP filegroup() Function
PHP fileinode() Function
PHP filemtime() Function
PHP fileowner() Function
PHP fileperms() Function
PHP filesize() Function
PHP filetype() Function
PHP flock() Function
PHP fnmatch() Function
PHP fopen() Function
PHP fpassthru() Function
PHP fprintf() Function
PHP fputcsv() Function
PHP fputs() Function
PHP fread() Function
PHP fscanf() Function
PHP fseek() Function
PHP fstat() Function
PHP ftell() Function
PHP ftruncate() Function
PHP fwrite() Function
PHP getcwd() function
PHP glob() Function
PHP is_dir() Function
PHP is_executable() Function
PHP is_file() Function
PHP is_link() Function
PHP is_readable() Function
PHP is_uploaded_file() Function
PHP is_writable() Function
PHP link() Function
PHP linkinfo() Function
PHP lstat() Function
PHP md5_file() Function
PHP mkdir() Function
PHP move_uploaded_file() Function
PHP opendir() Function
PHP parse_ini_file() Function
PHP pathinfo() Function
PHP pclose() Function
PHP popen() Function
PHP readdir() Function
PHP readfile() Function
PHP readlink() Function
PHP realpath() Function
PHP rename() Function
PHP rewind() Function
PHP rewinddir() Function
PHP rmdir() Function
PHP scandir() Function
PHP set_file_buffer() Function
PHP sha1_file() Function
PHP stat() Function
PHP symlink() Function
PHP tempnam() Function
PHP tmpfile() Function
PHP touch() Function
PHP umask() Function
PHP unlink() Function
PHP vfprintf() Function