sha1_file() Function in PHP 8.4 & PHP 8.5

In PHP 8.4 and 8.5, sha1_file() works the same as in earlier versions for normal files, but it now returns false when a stream read fails, instead of returning a hash for incomplete data.

Functionality

It calculates the SHA-1 hash of a file using the US Secure Hash Algorithm 1. The result is a 40-character hexadecimal string by default.

Example

<?php
$hash = sha1_file( /path/to/file.txt );
echo $hash; // e.g., "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"
?>

Change in PHP 8.4/8.5

The important fix is about incomplete reads. Previously, if the stream failed mid-read, sha1_file() might still return a digest for partial data. Now, it checks whether the stream reached EOF (end-of-file). If a read fails (n < 0 or the stream is not at EOF), the function returns false.
 

13 Views