php上传图片重命名的六种方法

WBOY
Freigeben: 2016-07-25 08:55:36
Original
1832 Leute haben es durchsucht
  1. /*
  2. com_create_guid()是php5版本支持的功能,对于不支持的版本,可以自己进行定义;
  3. */
  4. function guid(){
  5. if (function_exists('com_create_guid')){
  6. return com_create_guid();
  7. }else{
  8. mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
  9. echo(mt_rand());
  10. $charid = strtoupper(md5(uniqid(rand(), true)));
  11. $hyphen = chr(45);// "-"
  12. $uuid = chr(123)// "{"
  13. .substr($charid, 0, 8).$hyphen
  14. .substr($charid, 8, 4).$hyphen
  15. .substr($charid,12, 4).$hyphen
  16. .substr($charid,16, 4).$hyphen
  17. .substr($charid,20,12)
  18. .chr(125);// "}"
  19. return $uuid;
  20. }
  21. }
复制代码

2,MD5: 与guid 一样会输出32 字符十六进制数,区别是guid是随机产生的,md5需要根据输入的数据生成。 例子:

  1. $str = "Hello";
  2. echo md5($str);
  3. ?>
复制代码

输出结果: 8b1a9953c4611296a827abf8c47804d7 优点:可以根据输入的种子数据来控制输出的数值,如果种子数据是规律性不重复的,通过md5可以对数据进行保护,产生很大的混淆作用。 缺点:32位字符过长;需提供不重复的种子数据; 用法:高并发,以秒为种子数据,仍然会出现重复现象。 例子:

  1. /*
  2. *结合time()函数使用,以1970年到当前时间的秒数作为种子数。
  3. */
  4. $str=time();
  5. echo md5($str);
  6. ?>
复制代码

3,uniqid():返回13或23位字符串 对于我们目的来说,uniqid()像是md5()的改进版,尤其是我们可以采用差异性标识作为字符串前缀,可以降低重复命名出现的几率。 对于非高并发等极端情况,推荐使用此函数,已经可以满足一般性需求。

定义:uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID。 用法:uniqid(prefix,more_entropy) 说明:prefix可以为输出的字符串添加前缀,示例如下,more_entropy参数为true时,将输出23位字符串。

例子:

  1. var_dump(uniqid());
  2. var_dump(uniqid("a"));
  3. ?>
复制代码

输出结果: string(13) "51734aa562254" string(14) "a51734aa562257" 优点:13位字符串长度,是可以接受的文件命名长度;可以添加前缀,结果包含数据混淆,能够避免反推原始数据。 缺点:同md5相似,高并发,以秒为种子数据,仍然会出现重复现象。

三、升级版方案: 1,fast_uuid:返回17位数字 有点像uniqid()的不完全定制版,这个函数里面出现的“种子数开始时间”概念很有启发性。 time()和uniqid()中默认用到的时间都是从1970年开始计算的,长度有十位(1366512439),采用“种子数开始时间”能够缩小这个数值。 因为,从实际需求来看,仅仅是一个能够自动增长的数值即可。 起始时间自定义以后,除了减少长度,还能够起到混淆的作用。

例子:

  1. /*

  2. * 参数 suffix_len指定 生成的 ID 值附加多少位随机数,默认值为 3。
  3. * 感谢“Ivan Tan|谭俊青 DrinChing (at) Gmail.com”提供的算法。
  4. * @param int suffix_len
  5. * @return string
  6. */
  7. function fast_uuid($suffix_len=3){
  8. //! 计算种子数的开始时间
  9. $being_timestamp = strtotime('2013-3-21');
  10. $time = explode(' ', microtime());

  11. $id = ($time[1] - $being_timestamp) . sprintf('%06u', substr($time[0], 2, 6));
  12. if ($suffix_len > 0)
  13. {
  14. $id .= substr(sprintf('%010u', mt_rand()), 0, $suffix_len);
  15. }
  16. return $id;
  17. }
复制代码

输出结果: 29832412631099013

2,time()+随机数: 上例中已出现了随机数的使用,是为了解决一秒下发生的多次请求。 提供两个函数:

  1. function random($length) {
  2. $hash = '';
  3. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  4. $max = strlen($chars) - 1;
  5. PHP_VERSION for($i = 0; $i $hash .= $chars[mt_rand(0, $max)];
  6. } // bbs.it-home.org
  7. return $hash;
  8. }
  9. function random2($length, $numeric = 0) {
  10. PHP_VERSION $seed = base_convert(md5(print_r($_SERVER, 1).microtime()), 16, $numeric ? 10 : 35);
  11. $seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
  12. $hash = '';
  13. $max = strlen($seed) - 1;
  14. for($i = 0; $i $hash .= $seed[mt_rand(0, $max)];
  15. }
  16. return $hash;
  17. }
  18. ?>
复制代码

四、最终方案: 思路:userid+秒+随机数。其中“userid+秒”10进制转64进制,缩减位数; 说明: userid: 64进制最大值“ZZZZ”转换为十进制等于”16777215“,”ZZZ“转换为十进制最大值等于”262143“; 秒:设置自己的时间起点。

  1. const KeyCode = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$';

  2. /**
  3. * 将64进制的数字字符串转为10进制的数字字符串
  4. * @param $m string 64进制的数字字符串
  5. * @param $len integer 返回字符串长度,如果长度不够用0填充,0为不填充
  6. * @return string
  7. * @author 野马
  8. */
  9. function hex64to10($m, $len = 0) {
  10. $m = (string)$m;
  11. $hex2 = '';
  12. $Code = KeyCode;
  13. for($i = 0, $l = strlen($Code); $i $KeyCode[] = $Code[$i];
  14. }
  15. $KeyCode = array_flip($KeyCode);
  16. for($i = 0, $l = strlen($m); $i $one = $m[$i];

  17. $hex2 .= str_pad(decbin($KeyCode[$one]), 6, '0', STR_PAD_LEFT);
  18. }
  19. $return = bindec($hex2);
  20. if($len) {

  21. $clen = strlen($return);
  22. if($clen >= $len) {
  23. return $return;
  24. }
  25. else {
  26. return str_pad($return, $len, '0', STR_PAD_LEFT);
  27. }
  28. }
  29. return $return;
  30. }
  31. /**

  32. * 将10进制的数字字符串转为64进制的数字字符串
  33. * @param $m string 10进制的数字字符串
  34. * @param $len integer 返回字符串长度,如果长度不够用0填充,0为不填充
  35. * @return string
  36. * @author 野马
  37. */
  38. function hex10to64($m, $len = 0) {
  39. $KeyCode = KeyCode;
  40. $hex2 = decbin($m);
  41. $hex2 = str_rsplit($hex2, 6);
  42. $hex64 = array();
  43. foreach($hex2 as $one) {
  44. $t = bindec($one);
  45. $hex64[] = $KeyCode[$t];
  46. }
  47. $return = preg_replace('/^0*/', '', implode('', $hex64));
  48. if($len) {
  49. $clen = strlen($return);
  50. if($clen >= $len) {
  51. return $return;
  52. }
  53. else {
  54. return str_pad($return, $len, '0', STR_PAD_LEFT);
  55. }
  56. }
  57. return $return;
  58. }
  59. /**

  60. * 将16进制的数字字符串转为64进制的数字字符串
  61. * @param $m string 16进制的数字字符串
  62. * @param $len integer 返回字符串长度,如果长度不够用0填充,0为不填充
  63. * @return string
  64. * @author 野马
  65. */
  66. function hex16to64($m, $len = 0) {
  67. $KeyCode = KeyCode;
  68. $hex2 = array();
  69. for($i = 0, $j = strlen($m); $i $hex2[] = str_pad(base_convert($m[$i], 16, 2), 4, '0', STR_PAD_LEFT);
  70. }
  71. $hex2 = implode('', $hex2);
  72. $hex2 = str_rsplit($hex2, 6);
  73. foreach($hex2 as $one) {
  74. $hex64[] = $KeyCode[bindec($one)];
  75. }
  76. $return = preg_replace('/^0*/', '', implode('', $hex64));
  77. if($len) {
  78. $clen = strlen($return);
  79. if($clen >= $len) {
  80. return $return;
  81. }
  82. else {
  83. return str_pad($return, $len, '0', STR_PAD_LEFT);
  84. }
  85. }
  86. return $return;
  87. }
  88. /**

  89. * 功能和PHP原生函数str_split接近,只是从尾部开始计数切割
  90. * @param $str string 需要切割的字符串
  91. * @param $len integer 每段字符串的长度
  92. * @return array
  93. * @author 野马
  94. */
  95. function str_rsplit($str, $len = 1) {
  96. if($str == null || $str == false || $str == '') return false;
  97. $strlen = strlen($str);
  98. if($strlen $headlen = $strlen % $len;
  99. if($headlen == 0) {
  100. return str_split($str, $len);
  101. }
  102. $return = array(substr($str, 0, $headlen));
  103. return array_merge($return, str_split(substr($str, $headlen), $len));
  104. }
  105. $a=idate("U");

  106. echo "\r\n
    e:" . hex10to64($a);
  107. echo "\r\n
    e:" . hex64to10(hex10to64($a));
复制代码

算法2:

  1. function dec2s4($dec) {

  2. $base = '0123456789_$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  3. $result = '';
  4. do {

  5. $result = $base[$dec % 64] . $result;
  6. $dec = intval($dec / 64);
  7. } while ($dec != 0);
  8. return $result;

  9. }
  10. function s42dec($sixty_four) {

  11. $base_map = array ( '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, '_' => 10, '$' => 11, 'a' => 12, 'b' => 13, 'c' => 14, 'd' => 15, 'e' => 16, 'f' => 17, 'g' => 18, 'h' => 19, 'i' => 20, 'j' => 21, 'k' => 22, 'l' => 23, 'm' => 24, 'n' => 25, 'o' => 26, 'p' => 27, 'q' => 28, 'r' => 29, 's' => 30, 't' => 31, 'u' => 32, 'v' => 33, 'w' => 34, 'x' => 35, 'y' => 36, 'z' => 37, 'A' => 38, 'B' => 39, 'C' => 40, 'D' => 41, 'E' => 42, 'F' => 43, 'G' => 44, 'H' => 45, 'I' => 46, 'J' => 47, 'K' => 48, 'L' => 49, 'M' => 50, 'N' => 51, 'O' => 52, 'P' => 53, 'Q' => 54, 'R' => 55, 'S' => 56, 'T' => 57, 'U' => 58, 'V' => 59, 'W' => 60, 'X' => 61, 'Y' => 62, 'Z' => 63, );
  12. $result = 0;
  13. $len = strlen($sixty_four);
  14. for ($n = 0; $n $result *= 64;

  15. $result += $base_map[$sixty_four{$n}];
  16. }
  17. return $result;

  18. }
  19. $a=idate("U");

  20. var_dump(dec2s4($a));
  21. var_dump(s42dec(dec2s4($a)));
复制代码

算法效率测试:

  1. $strarr = array();

  2. $time1 = microtime(true);
  3. for($i = 0; $i $str = idate("U")+$i;
  4. $strarr[] = "{$i}->$str\r\n
    ";
  5. }
  6. $time2 = microtime(true);
  7. $time3 = $time2 - $time1;
  8. $time1 = microtime(true);

  9. for($i = 0; $i $str = dec2s4(idate("U")+$i);
  10. $strarr[] = "{$i}->$str\r\n
    ";
  11. }
  12. $time2 = microtime(true);
  13. echo "\r\n
    运行10000次用时(秒):" . ($time2 - $time1 - $time3);
复制代码

测试结果: 算法1:0.1687250137329 算法2:0.044965028762817 结论:算法1虽然效率上差一些,但是可以把md5生成的16进制转化为64进制,能够使用在必须使用md5的环境下缩短字符串。

六、总结 本文内容的关键点是使用10进制转换为64进制来进行字符串的缩减。 例如,使用fast_uuid生成的17位数字,转换为64进制仅有7位字符;

以上就是有关php上传图片重命名的全部内容了,希望对大家有所帮助。



Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!