W3docs

sha1_file()

Our article is about the PHP function sha1_file(), which is used to calculate the SHA-1 hash of a file. This function is useful for verifying file integrity and

The PHP sha1_file() function calculates the SHA-1 hash of a file. It is useful for verifying file integrity and ensuring secure storage and transmission of data. Below is the syntax and usage examples.

Syntax

string sha1_file ( string $filename [, bool $raw_output = false ] )

The function takes two parameters: $filename and $raw_output. The $filename parameter is the name of the file to be hashed. The $raw_output parameter is optional and specifies whether to output raw binary data or a string of hexadecimal characters.

Here is an example of how to use the sha1_file() function:

Example

<?php
$filename = 'example.txt';
$hash = sha1_file($filename);
if ($hash !== false) {
    echo $hash;
} else {
    echo "File not found or unreadable.";
}
?>

This example checks if example.txt exists and calculates its SHA-1 hash.

The output of this code will be:

3d34c2308ff506e3f7a945e6208cd63a4e7d29

As you can see, the sha1_file() function has calculated the SHA-1 hash of the file.

The sha1_file() function is a useful tool for verifying file integrity and ensuring secure data storage and transmission in PHP. It calculates a SHA-1 hash, which is a cryptographic hash function that generates a unique, fixed-length output based on the file's contents. By mastering this function, you can become a more proficient PHP developer.

We hope this article has been helpful in understanding the sha1_file() function in PHP.

Practice

Practice

What does the sha1_file() function in PHP do?