w3resource

PHP: sha1_file() function

Description

The sha1_file() function is used to calculate the sha1 hash (Using US Secure Hash Algorithm 1 ) of a file.

Version:

(PHP 4 and above)

Syntax:

sha1_file (file_name, raw_output)

Parameter:

Name Description Required /
Optional
Type
file_name The input file name. Required String
raw_output If TRUE sets raw 20 characters binary format. If FALSE (default) sets raw 40 character hex number format. Optional Boolean

Return value:

Returns a string on success, FALSE otherwise.

Value Type: String.

Pictorial Presentation

php-string-sha1_file()

Example:

<?php
$file_handler = 'testvf.txt';
$sha1_file = sha1_file($file_handler);
echo $sha1_file;
?>

Output:

833cd96367d51bb065f486f4b2bd7fe8bcb70996

View the example in the browser

See also

PHP Function Reference

Previous: rtrim
Next: sha1



PHP: Tips of the Day

Deep flattens an array

Example:

<?php
function tips_deepFlatten($items)
{
  $result = [];
  foreach ($items as $item) {
    if (!is_array($item)) {
      $result[] = $item;
    } else {
      array_push($result, ...tips_deepFlatten($item));
    }
  }

  return $result;
}

Print_r(tips_deepFlatten([1, [2], [[3], 4], 5]));
?>

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)