PHP Function Reference

PHP hash_file() Function



The PHP hash_file() function generates a hash value using the contents of a given file.

Syntax

hash_file(algo, filename, binary)

Parameters

algo Required. Specify the name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..). A list of supported algorithms can be found using hash_algos() function.
filename Required. Specify URL describing location of file to be hashed. It supports fopen wrappers.
binary Optional. If set to true, outputs raw binary data. Default is false which outputs lowercase hexits.

Return Value

Returns a string containing the calculated message digest as lowercase hexits unless binary is set to true in which case the raw binary representation of the message digest is returned.

Example: hash_file() example

The example below shows the usage of hash_file() function.

<?php
//creating a file to calculate hash of
file_put_contents('test.txt', 'Hello World!');

//displaying the result
echo hash_file('sha1', 'test.txt');
?>

The output of the above code will be:

2ef7bde608ce5404e97d5f042f95f89f1c232871

❮ PHP Hash Reference