PHP hash_file() Function

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.
$digest = hash_file('sha256', $path);
| Call | What 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
hash_file(string $algo, string $filename, bool $binary = false, array $options = []): string|false
Parameters
| Parameter | Description |
|---|
| algo | Required. The algorithm name, such as "sha256". |
| filename | Required. The path to the file. URL wrappers are supported. |
| binary | Optional. false (default) returns hex. true returns raw bytes. |
| options | Optional. PHP 8.1+. Extra settings for some algorithms. |
Return Value
| Return Value | Description |
|---|
| string|false | The 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
$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);
027bd65edfc2f0baf383db119d8a4a1a3c70697c717c9179a6903411b8109467
Same as hash(): yes
How It Works
- Create a temp file. It holds a known text.
- Call `hash_file()`. It reads the whole file.
- Compare with `hash()`. The text gives the same hash.
- Delete the file. Clean up after the demo.
Example 2 — A Missing File Returns false
Always check the result.
<?php
$digest = @hash_file('sha256', '/no/such/file.txt');
if ($digest === false) {
echo "File not found", PHP_EOL;
}
How It Works
- Point to a bad path. No such file exists.
- Silence the warning with @. Only for this demo.
- Test with === false. A hash string is never false.
- Handle the error. Show a message or log it.
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.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
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