doulai5585 2015-11-16 11:11
浏览 71
已采纳

我可以将MD5 +后缀用于自定义长度的唯一ID吗?

I want to use it for data name, such as

// Get the file
$file = open('abc.txt');

// Get MD5 Hash value from the file content
// It should be 32 length and Append 2 more(suffix)
$name = MD5_from_file($file) + 'AZ';

// Rename the file as $name
rename($file, $name)

// Then, the file name like this: 9E107D9D372BB6826BD81D3542A419D6AZ.txt
  • 写回答

1条回答 默认 最新

  • dongzhun1857 2015-11-16 11:25
    关注

    It's even simpler. Use sha1_file() instead of md5(). You really dont need a suffix, the hash value sha1 is designed to be unique and dependent on a concrete file. The likelyhood of two files having the same hash value is really minimal and as close to 0 as on will get.

    <?php
    $newName = sha1_file('abc.txt');
    rename($file, $newName);
    ?>
    

    Length of sha1() will be 40 chars.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集