Content · Programming · PHP

PHP hash_file() Function: Usage and Examples

Hashes the bytes of a file using a chosen digest algorithm.

1 views Published Oct 17, 2026 Updated Oct 17, 2026

hash_file() Function is one of the PHP operations used in filesystem and stream workflows. Hashes the bytes of a file using a chosen digest algorithm. The examples below show what it does and how to handle its result.

What Is hash_file() Function?


Hashes the bytes of a file using a chosen digest algorithm.

Its syntax is:

hash_file(string $algo, string $filename, bool $binary = false, array $options = [])

The parameters are:

  • string $algo — Hash algorithm name, such as sha256.
  • string $filename — The source or destination filesystem path.
  • bool $binary = false — When true, returns raw digest bytes rather than hexadecimal text.
  • array $options = [] — Optional configuration flags or parsing settings.

Return value: The declared return type is string|false. A checksum detects accidental changes but does not prove authenticity without a trusted reference.

Important: A checksum detects accidental changes but does not prove authenticity without a trusted reference.

Example 1: Basic Usage


The basic example focuses on the behavior of hash_file() Function: hashes the bytes of a file using a chosen digest algorithm. Inputs are defined locally or shown as application-controlled paths.

<?php

$digest = hash_file("sha256", __FILE__);
if ($digest !== false) { echo $digest; }

The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.

Example 2: A Different Use Case


This second example illustrates another input or execution condition for hash_file() Function. Notice the different checks, arguments, or output handling.

<?php

$expectedHash = "approved-sha256-digest";
$actualHash = hash_file("sha256", __FILE__);
if ($actualHash !== false && hash_equals($expectedHash, $actualHash)) { echo "File matches"; }

This variation matters because a checksum detects accidental changes but does not prove authenticity without a trusted reference.

Practical Example: Using the Operation in an Application


The following example demonstrates how hash_file() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.

<?php

$path = __DIR__ . "/release.zip";
if (!is_file($path)) { throw new RuntimeException("Package not found"); }
$sha256 = hash_file("sha256", $path);
if ($sha256 === false) { throw new RuntimeException("Hash failed"); }
echo $sha256;

The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.

Important Behavior and Edge Cases


  • Function behavior: Hashes the bytes of a file using a chosen digest algorithm.
  • Limitation: A checksum detects accidental changes but does not prove authenticity without a trusted reference.

Comparison With Related Functions


Function or MethodDifference
hash_file() FunctionHashes the bytes of a file using a chosen digest algorithm.
md5_file()Uses MD5, which has known collision weaknesses; hash_file() can use SHA-256.

Use hash_file() Function for the specific behavior described above. The related operation, md5_file(), serves a different purpose; do not substitute it unless that different behavior is intended.

Common Beginner Mistakes


MistakeCorrect Approach
Assuming this operation has the same behavior on every file or environment.A checksum detects accidental changes but does not prove authenticity without a trusted reference. Check prerequisites and return values before using the result.
Treating md5_file() as an interchangeable replacement for hash_file() Function.Uses MD5, which has known collision weaknesses; hash_file() can use SHA-256.

Key Takeaways


  • Hashes the bytes of a file using a chosen digest algorithm.
  • A checksum detects accidental changes but does not prove authenticity without a trusted reference.
  • Related operation: md5_file() — Uses MD5, which has known collision weaknesses; hash_file() can use SHA-256.

hash_file() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.

Share this resourceSend this page to someone who may find it useful.