PHP sha1_file() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, sha1_file() calculates the SHA-1 hash of a file's contents.

Pass a file path. It returns a 40-character hex hash, or false if the file cannot be read.

Syntax

Call sha1_file() with the values you need. Start with this line.

PHP
$hash = sha1_file($path);
CallWhat it does
sha1_file("report.pdf")Returns the 40-character hash of that file.
sha1_file("report.pdf", true)Returns 20 raw bytes.

The hash is based on file contents only, not on the file name or dates. If the file is missing, PHP gives a warning and returns false.

Library declarations
PHP
sha1_file(string $filename, bool $binary = false): string|false

Parameters

ParameterDescription
filenameRequired. Path to the file you want to hash.
binaryOptional. If true, returns raw 20-byte binary. Default is false.

Return Value

Return ValueDescription
stringThe SHA-1 hash of the file (40 hex characters by default).
falseReturned when the file cannot be read.

PHP Examples

Example 1 — Hash a Small File

Create a temporary file, then hash its contents.

PHP
<?php
$file = tempnam(sys_get_temp_dir(), "demo");
file_put_contents($file, "hello");

echo sha1_file($file), PHP_EOL;
unlink($file);

How It Works

  1. Make a temp file. tempnam() gives a safe path.
  2. Write text. The file now holds hello.
  3. Call sha1_file(). It reads the file and hashes it.
  4. Clean up. unlink() removes the temp file.

Example 2 — Handle a Missing File

A missing file returns false, so always check the result.

PHP
<?php
$hash = @sha1_file("missing-file.txt");

if ($hash === false) {
    echo "Could not read the file.", PHP_EOL;
} else {
    echo $hash, PHP_EOL;
}

How It Works

  1. Call with a bad path. The file does not exist.
  2. Silence the warning. The @ hides PHP's warning here.
  3. Check with ===. Strict comparison avoids confusion with other values.
  4. Show a friendly message. Your page keeps working.

Example 3 — Check If Two Files Match

Equal hashes mean the contents are the same.

PHP
<?php
$a = tempnam(sys_get_temp_dir(), "a");
$b = tempnam(sys_get_temp_dir(), "b");

file_put_contents($a, "same text");
file_put_contents($b, "same text");

echo sha1_file($a) === sha1_file($b) ? "Files match" : "Files differ", PHP_EOL;

unlink($a);
unlink($b);

How It Works

  1. Create two files. Both get the same text.
  2. Hash each file. Names differ, but contents are equal.
  3. Compare the hashes. Equal hashes mean matching content.
  4. Delete the temp files. Good habit for demo code.

Frequently Asked Questions

sha1_file() reads a file and returns the SHA-1 hash of its contents as a 40-character hex string. You do not need to load the file into a variable first.
sha1_file(string $filename, bool $binary = false): string|false. Pass the file path. Set the second argument to true for raw binary output.
PHP raises a warning and sha1_file() returns false. Check the result with === false before using it.
No. Only the file contents matter. Two files with different names but the same content give the same hash.
sha1() hashes a string you already have in memory. sha1_file() opens a file by path and hashes what is inside it.

Next: similar_text()

similar_text() counts how many characters two strings have in common.

similar_text() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful