給指定檔案的內容產生雜湊值

hash_file

(PHP 5 >= 5.1.2, PHP 7, PHP 8, PECL hash >= 1.1)

hash_file給指定檔案的內容產生雜湊值

說明

hash_file(string $algo, string $filename, bool $binary = false): string|false

參數

algo

要使用的雜湊演算法的名稱(例如:"md5","sha256","haval160,4" 等)。 可以在 hash_algos() 中檢視目前支援的演算法。

filename

要進行雜湊運算的檔案位置的 URL ;支援 fopen 封裝協議。

binary

設定為 true 時,輸出原始二進制數據。 設定為 false 時,輸出小寫的十六進制字串。

返回值

如果 binary 設定為 true, 則返回原始二進制數據表示的資訊摘要, 否則返回十六進制小寫字串格式表示的資訊摘要。

範例

示例 #1 使用 hash_file()

<?php
/* 建立一個要計算雜湊值的檔案 */
file_put_contents('example.txt''The quick brown fox jumped over the lazy dog.');
echo 
hash_file('md5''example.txt');
?>

以上例程會輸出:

5c6ffbdd40d9556b73a21e63c3e0e904

參見

Leave a Reply

Your email address will not be published. Required fields are marked *