百科狗-知识改变命运!
--

md5_file() - 计算指定文件的 MD5 散列值 - php 字符串函数

百变鹏仔1年前 (2023-11-21)阅读数 40#技术干货
文章标签字符串

md5_file()

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

计算指定文件的 MD5 散列值

说明

md5_file(string $filename[,bool $raw_output= FALSE] ): string

使用» RSA 数据安全公司的 MD5 报文算法计算$filename文件的 MD5 散列值并返回。该散列值为 32 字符的十六进制数字。

参数

$filename

文件名

$raw_output

如果被设置为TRUE,那么报文摘要将以原始的 16 位二进制格式返回。

返回值

md5_file() - 计算指定文件的 MD5 散列值 - php 字符串函数

成功返回字符串,否则返回FALSE

更新日志

版本说明
5.1.0函数改用流 API。这意味着能够配合封装器使用该函数,比如md5_file('http://example.com/..')

范例

Example #1md5_file()使用范例

参见

  • md5()计算字符串的 MD5 散列值
  • sha1_file()计算文件的 sha1 散列值
  • crc32()计算一个字符串的 crc32 多项式
If you just need to find out if two files are identical, comparing file hashes can be inefficient, especially on large files. There's no reason to read two whole files and do all the math if the second byte of each file is different. If you don't need to store the hash value for later use, there may not be a need to calculate the hash value just to compare files. This can be much faster:
It's faster to use md5sum than openssl md5:

About 3x faster
In response to using exec instead for performance (Nov 13 2007 post), It looks like the performance depends on the size of the file. See the results below using the same script from the original post. The first hash is with md5_file and the second is with openssl md5.
With a 1MB file:
Hash = df1555ec0c2d7fcad3a03770f9aa238a; time = 0.005006
Hash = df1555ec0c2d7fcad3a03770f9aa238a; time = 0.01498
With a 2MB file:
Hash = 4387904830a4245a8ab767e5937d722c; time = 0.010393
Hash = 4387904830a4245a8ab767e5937d722c; time = 0.016691
With a 10MB file:
Hash = b89f948e98f3a113dc13fdbd3bdb17ef; time = 0.241907
Hash = b89f948e98f3a113dc13fdbd3bdb17ef; time = 0.037597
Performance seems to change proportionally with the file size. Judging from the previous post's default file name (.mov) he/she was probably dealing with a large file. These are just quick tests and far from a perfect benchmark, but you might want to test your own files before assuming that the openssl solution is faster (ie, if working with small text files vs. movies, etc)
Heres a function to give an md5 for an entire directory..
function MD5_DIR($dir)
{
  if (!is_dir($dir))
  {
    return false;
  }
  
  $filemd5s = array();
  $d = dir($dir);
  while (false !== ($entry = $d->read()))
  {
    if ($entry != '.' && $entry != '..')
    {
       if (is_dir($dir.'/'.$entry))
       {
         $filemd5s[] = MD5_DIR($dir.'/'.$entry);
       }
       else
       {
         $filemd5s[] = md5_file($dir.'/'.$entry);
       }
     }
  }
  $d->close();
  return md5(implode('', $filemd5s));
}
For those of you with PHP 4 that want to output the "raw" 128 bit hash, all you need to do is send it to pack to convert the hex string into the raw output.
ie:
$filename="checkthisfile.bin";
$rawhash=pack("H*",md5_file($filename));

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)