PHP hash_file() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Hash

Definition

In PHP, hash_file() reads a file and returns a hash of its contents using the algorithm you choose.

It is handy for checking downloads and spotting when a file has changed. It reads in chunks, so big files are fine.

Syntax

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

PHP
$digest = hash_file('sha256', $path);
CallWhat it does
hash_file('sha256', 'report.pdf')Returns the 64-character hash of the file.
hash_file('md5', 'missing.txt')Returns false when the file cannot be read.

The hash covers only the contents, not the file name or dates.

Library declarations
PHP
hash_file(string $algo, string $filename, bool $binary = false, array $options = []): string|false

Parameters

ParameterDescription
algoRequired. The algorithm name, such as "sha256".
filenameRequired. The path to the file. URL wrappers are supported.
binaryOptional. false (default) returns hex. true returns raw bytes.
optionsOptional. PHP 8.1+. Extra settings for some algorithms.

Return Value

Return ValueDescription
string|falseThe hash of the file, or false if the file cannot be opened.

PHP Examples

Example 1 — Hash a File

Write a small temp file, hash it, and compare with hash() on the same text.

PHP
<?php
$file = tempnam(sys_get_temp_dir(), 'hf_');
file_put_contents($file, 'Hello, CodeToFun!');

$digest = hash_file('sha256', $file);

echo $digest, PHP_EOL;
echo "Same as hash(): ", $digest === hash('sha256', 'Hello, CodeToFun!') ? "yes" : "no", PHP_EOL;

unlink($file);

How It Works

  1. Create a temp file. It holds a known text.
  2. Call `hash_file()`. It reads the whole file.
  3. Compare with `hash()`. The text gives the same hash.
  4. Delete the file. Clean up after the demo.

Example 2 — A Missing File Returns false

Always check the result.

PHP
<?php
$digest = @hash_file('sha256', '/no/such/file.txt');

if ($digest === false) {
    echo "File not found", PHP_EOL;
}

How It Works

  1. Point to a bad path. No such file exists.
  2. Silence the warning with @. Only for this demo.
  3. Test with === false. A hash string is never false.
  4. Handle the error. Show a message or log it.

Example 3 — Detect a Changed File

Hash before and after an edit.

PHP
<?php
$file = tempnam(sys_get_temp_dir(), 'hf_');

file_put_contents($file, 'version 1');
$before = hash_file('sha256', $file);

file_put_contents($file, 'version 2');
$after = hash_file('sha256', $file);

echo "Changed: ", $before !== $after ? "yes" : "no", PHP_EOL;

unlink($file);

How It Works

  1. Write version 1. Save its hash.
  2. Overwrite the file. The text is now different.
  3. Hash again. A new hash appears.
  4. Compare. Different hashes mean the file changed.

Frequently Asked Questions

It reads a file and returns a hash of its contents, using the algorithm you name.
hash_file(string $algo, string $filename, bool $binary = false, array $options = []): string|false.
Yes. hash_file('sha256', $path) equals hash('sha256', file_get_contents($path)), but it uses less memory.
It returns false and raises a warning. Always check with === false.
Yes. PHP reads the file in small chunks, so memory use stays low even for very large files.

Next: hash_final()

hash_final() finishes an incremental hash and returns it.

hash_final() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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