Created
May 6, 2023 15:12
-
-
Save scmrus/5d7a302b102992a156a8b1c2a0936b6f to your computer and use it in GitHub Desktop.
MD5 hash for a large file in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function md5_file_large($file_path) { | |
if (!file_exists($file_path)) { | |
return false; | |
} | |
$chunk_size = 1024 * 1024; // 1MB | |
$md5_hash = hash_init('md5'); | |
$file = fopen($file_path, 'rb'); | |
if ($file === false) { | |
return false; | |
} | |
while (!feof($file)) { | |
$buffer = fread($file, $chunk_size); | |
hash_update($md5_hash, $buffer); | |
} | |
fclose($file); | |
return hash_final($md5_hash); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment