Skip to content

Instantly share code, notes, and snippets.

@scmrus
Created May 6, 2023 15:12
Show Gist options
  • Save scmrus/5d7a302b102992a156a8b1c2a0936b6f to your computer and use it in GitHub Desktop.
Save scmrus/5d7a302b102992a156a8b1c2a0936b6f to your computer and use it in GitHub Desktop.
MD5 hash for a large file in PHP
<?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