doumeikuan6834 2017-09-24 13:35
浏览 13
已采纳

从uniqid只返回4个字符

Currently I am using the following code to generate a random name for my uploaded file.

$file_upload_name = uniqid('', true) . '.' . $file_type; 

This returns something that looks like this 59c7b4868f4776.27613088.mp3 how can I turn this into something that looks like this 5vh2.mp3 so that it looks cleaner when presented to a user?

  • 写回答

2条回答 默认 最新

  • dtdt0454 2017-09-24 13:45
    关注

    EDIT: a good point from salathe in the comments:

    The first 8 characters of the uniqid() return value (minus any provided prefix) represent the Unix timestamp at the time of the function call. Using only the first 4 characters means that the value will only change every 2^16 seconds (~18 hours). For example, if this code were executed at any time between (and including) 2017-09-24T00:44:48Z and 2017-09-24T18:57:03Z it would return the same "unique" value.

    You may will have collision issues with only four characters considering the first 4 values will only change around every 18 hours (assuming you are calling this script at least 2 two times within 18 hours).

    Using the last four characters would be more 'unique'. If you need to use the uniq function you can use the substr function.

    $unique = uniqid('', true);
    $file_name = substr($unique, strlen($unique) - 4, strlen($unique));
    $file_upload_name = $file_name . "." . $file_type;
    

    Be aware having videos with only four characters lessens the uniqueness of video IDs and increases the collision rate of a generated file name being the same.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路