PHP md5_file() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, md5_file() calculates the MD5 hash of a file.

It is handy for checking whether a file has changed or was downloaded correctly.

Syntax

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

PHP
$hash = md5_file("report.pdf");
CallWhat it does
md5_file("report.pdf")Returns the 32-character hash of the file.
md5_file("report.pdf", true)Returns the raw 16-byte hash.
md5_file("missing.txt")Returns false and raises a warning.

The file is read in chunks, so large files do not need to fit in memory.

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

Parameters

ParameterDescription
filenameRequired. The path to the file.
binaryOptional. If true, returns 16 raw bytes. Default is false, which returns 32 hex characters.

Return Value

Return ValueDescription
stringThe MD5 hash of the file.
falseReturned when the file cannot be read.

PHP Examples

Example 1 — Hash a File

Create a small temporary file, then hash it.

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

echo md5_file($file), PHP_EOL;

unlink($file);

How It Works

  1. Make a temp file. tempnam gives a safe unique path.
  2. Write some text. The file now contains hello.
  3. Call md5_file. It hashes the file contents.
  4. Clean up. unlink deletes the temporary file.

Example 2 — A File That Does Not Exist

A missing file returns false instead of a hash.

PHP
<?php
$hash = @md5_file("/no/such/file.txt");

var_dump($hash);

How It Works

  1. Use a bad path. The file does not exist.
  2. Silence the warning. The @ sign hides it in this demo.
  3. Get false. md5_file cannot read the file.
  4. Check in real code. Test the result before you use it.

Example 3 — File Hash vs String Hash

A file hash matches md5() of the same bytes.

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

$fileHash = md5_file($file);
$textHash = md5("hello");

echo $fileHash === $textHash ? "Same hash" : "Different hash", PHP_EOL;

unlink($file);

How It Works

  1. Write known text. The file holds exactly hello.
  2. Hash both ways. One uses md5_file, the other uses md5.
  3. Compare with ===. The values are equal.
  4. Why it matters. Only the bytes matter, not where they come from.

Frequently Asked Questions

md5_file() reads a file and returns its MD5 hash as 32 hexadecimal characters, or 16 raw bytes if the binary flag is true.
string|false md5_file(string $filename, bool $binary = false).
It returns false and raises a warning. Check the return value with === false.
md5() hashes a string you already have. md5_file() takes a file path and reads the file for you.
Yes, for catching accidental corruption. For security checks, use hash_file() with sha256 instead.

Next: metaphone()

metaphone() turns a word into a short code based on how it sounds in English.

metaphone() 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