這可能很明顯,但 hash_update_stream() 會移動檔案指標。因此,如果您打算在雜湊後讀取檔案,請使用 rewind()。
(PHP 5 >= 5.1.2, PHP 7, PHP 8, PECL hash >= 1.1)
hash_update_stream — 將資料從已開啟的串流引入作用中的雜湊內容
從 stream
實際新增至雜湊演算法的位元組數。
範例 #1 hash_update_stream() 範例
<?php
$fp = tmpfile();
fwrite($fp, 'jumped over the lazy dog.');
rewind($fp);
$ctx = hash_init('sha256');
hash_update($ctx, 'The quick brown fox ');
hash_update_stream($ctx, $fp);
echo hash_final($ctx);
?>
上述範例將輸出
68b1282b91de2c054c36629cb8dd447f12f096d3e3c587978dc2248444633483