Six ways to rename PHP upload images

Source: Internet
Author: User
Tags idate
    1. /*
    2. Com_create_guid () is a feature supported by the PHP5 version and can be defined on its own for unsupported versions;
    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, 4). $hyphen
    16. . substr ($charid, 4). $hyphen
    17. . substr ($charid, 20,12)
    18. . chr (125);//"}"
    19. return $uuid;
    20. }
    21. }
Copy Code

2,MD5: The same as the GUID will output 32 character hexadecimal number, the difference is that the GUID is randomly generated, MD5 need to be generated according to the input data. Example:

    1. $str = "Hello";
    2. echo MD5 ($STR);
    3. ?>
Copy Code

Output: 8b1a9953c4611296a827abf8c47804d7 Advantages: Can be based on the input of the seed data to control the output value, if the seed data is not repeated regularly, through the MD5 can protect the data, resulting in a great confusion. Cons: 32-bit characters are too long, you need to provide non-repeating seed data; usage: High concurrency, with seconds as seed data, there will still be duplicates. Example:

    1. /*
    2. * Combined with the time () function, the number of seconds from 1970 to current is the seed count.
    3. */
    4. $str =time ();
    5. echo MD5 ($STR);
    6. ?>
Copy Code

3,uniqid (): Returns a 13 or 23-bit string for our purposes, uniqid () is like an improved version of MD5 (), especially if we can use the differential identifier as a string prefix to reduce the chance of duplicate naming occurrences. For extreme situations such as non-high concurrency, it is recommended to use this function to meet general requirements.

Definition: the Uniqid () function generates a unique ID based on the current time in microseconds. Usage: uniqid (prefix,more_entropy) Description: Prefix can add a prefix to the output string, as shown below, when the More_entropy parameter is true, the 23-bit string is output.

Example:

    1. Var_dump (Uniqid ());
    2. Var_dump (Uniqid ("a"));
    3. ?>
Copy Code

Output: String "51734aa562254" string "a51734aa562257" advantage: 13-bit string length, is an acceptable file name length, you can add a prefix, the result contains data confusion, to avoid the reverse of the original data. Disadvantage: Similar to MD5, high concurrency, in seconds as the seed data, there will still be a repetition phenomenon.

Third, the upgrade version of the scheme: 1,fast_uuid: Return 17 digits a bit like the uniqid () of the incomplete custom version, the function of the "seed number start time" concept is very enlightening. Time () and uniqid () are used by default in 1970, with a length of 10 bits (1366512439) and a "seed count start time" to narrow this value. Because, in terms of actual demand, it is only a numerical value that can be automatically increased. Since the start time customization, in addition to reducing the length, but also can play a role of confusion.

Example:

    1. /*

    2. * Parameter Suffix_len Specifies how many bits of random number are attached to the generated ID value, and the default value is 3.
    3. * Thanks to "Ivan tan| Tan Junqing drinching (at) gmail.com" provides the algorithm.
    4. * @param int Suffix_len
    5. * @return String
    6. */
    7. function Fast_uuid ($suffix _len=3) {
    8. //! Calculate the start time of the seed number
    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. }

Copy Code

Output results: 29832412631099013

2,time () + random number: The use of random numbers has occurred in the above example in order to resolve multiple requests that occur in one second. provides two functions:

  1. function Random ($length) {
  2. $hash = ";
  3. $chars = ' abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ';
  4. $max = strlen ($chars)-1;
  5. Php_version < ' 4.2.0 ' && Mt_srand ((double) microtime () * 1000000);
  6. for ($i = 0; $i < $length; $i + +) {
  7. $hash. = $chars [Mt_rand (0, $max)];
  8. }//Bbs.it-home.org
  9. return $hash;
  10. }
  11. function Random2 ($length, $numeric = 0) {
  12. Php_version < ' 4.2.0 '? Mt_srand (Double) microtime () * 1000000): Mt_srand ();
  13. $seed = Base_convert (MD5 (Print_r ($_server, 1). Microtime ()), $numeric? 10:35);
  14. $seed = $numeric? (Str_replace (' 0 ', ', $seed). ' 012340567890 '): ($seed. ' ZZ '. Strtoupper ($seed));
  15. $hash = ";
  16. $max = strlen ($seed)-1;
  17. for ($i = 0; $i < $length; $i + +) {
  18. $hash. = $seed [Mt_rand (0, $max)];
  19. }
  20. return $hash;
  21. }
  22. ?>
Copy Code

Iv. Final Solution: userid+ seconds + random number. Wherein "userid+ seconds" 10 binary to 64, reducing the number of bits; Description: userid:64 binary maximum "ZZZZ" is converted to decimal equals "16777215", "ZZZ" is converted to decimal maximum equals "262143"; seconds: sets its own time starting point.

  1. Const KEYCODE = ' 0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz_$ ';

  2. /**
  3. * Converts a 64-digit string into a 10-binary numeric string
  4. * @param string of $m string 64 binary
  5. * @param $len Integer Returns the length of the string, if the length is not sufficient with 0 padding, 0 is not populated
  6. * @return String
  7. * @author Mustang
  8. */
  9. function Hex64to10 ($m, $len = 0) {
  10. $m = (string) $m;
  11. $hex 2 = ";
  12. $Code = KeyCode;
  13. for ($i = 0, $l = strlen ($Code); $i < $l; $i + +) {
  14. $KeyCode [] = $Code [$i];
  15. }
  16. $KeyCode = Array_flip ($KeyCode);

  17. for ($i = 0, $l = strlen ($m); $i < $l; $i + +) {

  18. $one = $m [$i];
  19. $hex 2. = Str_pad (Decbin ($KeyCode [$one]), 6, ' 0 ', str_pad_left);
  20. }
  21. $return = Bindec ($hex 2);

  22. if ($len) {

  23. $clen = strlen ($return);
  24. if ($clen >= $len) {
  25. return $return;
  26. }
  27. else {
  28. Return Str_pad ($return, $len, ' 0 ', str_pad_left);
  29. }
  30. }
  31. return $return;
  32. }

  33. /**

  34. * Converts a 10-digit string into a 64-binary numeric string
  35. * @param string of $m string 10 binary
  36. * @param $len Integer Returns the length of the string, if the length is not sufficient with 0 padding, 0 is not populated
  37. * @return String
  38. * @author Mustang
  39. */
  40. function Hex10to64 ($m, $len = 0) {
  41. $KeyCode = KeyCode;
  42. $hex 2 = Decbin ($m);
  43. $hex 2 = Str_rsplit ($hex 2, 6);
  44. $hex = Array ();
  45. foreach ($hex 2 as $one) {
  46. $t = Bindec ($one);
  47. $hex 64[] = $KeyCode [$t];
  48. }
  49. $return = preg_replace ('/^0*/', ' ', implode (', $hex 64));
  50. if ($len) {
  51. $clen = strlen ($return);
  52. if ($clen >= $len) {
  53. return $return;
  54. }
  55. else {
  56. Return Str_pad ($return, $len, ' 0 ', str_pad_left);
  57. }
  58. }
  59. return $return;
  60. }

  61. /**

  62. * Converts a 16-digit string into a 64-binary numeric string
  63. * @param string of $m string 16 binary
  64. * @param $len Integer Returns the length of the string, if the length is not sufficient with 0 padding, 0 is not populated
  65. * @return String
  66. * @author Mustang
  67. */
  68. function Hex16to64 ($m, $len = 0) {
  69. $KeyCode = KeyCode;
  70. $hex 2 = array ();
  71. for ($i = 0, $j = strlen ($m), $i < $j; + + $i) {
  72. $hex 2[] = Str_pad (Base_convert ($m [$i], 2), 4, ' 0 ', str_pad_left);
  73. }
  74. $hex 2 = Implode ("', $hex 2);
  75. $hex 2 = Str_rsplit ($hex 2, 6);
  76. foreach ($hex 2 as $one) {
  77. $hex 64[] = $KeyCode [Bindec ($one)];
  78. }
  79. $return = preg_replace ('/^0*/', ' ', implode (', $hex 64));
  80. if ($len) {
  81. $clen = strlen ($return);
  82. if ($clen >= $len) {
  83. return $return;
  84. }
  85. else {
  86. Return Str_pad ($return, $len, ' 0 ', str_pad_left);
  87. }
  88. }
  89. return $return;
  90. }

  91. /**

  92. * Function and PHP native function Str_split Close, just start counting cutting from the tail
  93. * @param string to be cut $str string
  94. * @param $len length of each string of integers
  95. * @return Array
  96. * @author Mustang
  97. */
  98. function Str_rsplit ($str, $len = 1) {
  99. if ($str = = NULL | | $str = = FALSE | | $str = = ") return false;
  100. $strlen = strlen ($STR);
  101. if ($strlen <= $len) return Array ($STR);
  102. $headlen = $strlen% $len;
  103. if ($headlen = = 0) {
  104. Return Str_split ($str, $len);
  105. }
  106. $return = Array (substr ($str, 0, $headlen));
  107. Return Array_merge ($return, Str_split (substr ($str, $headlen), $len));
  108. }

  109. $a =idate ("U");

  110. echo "\ r \ n
    E: ". Hex10to64 ($a);
  111. echo "\ r \ n
    E: ". Hex64to10 (hex10to64 ($a));

Copy Code

Algorithm 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, ' _ ' and ' + ', ' $ ', ' + ', ' a ', ' + ', ' B ' + ', ' c ' + =, ' d ' = The ' f ' = +, ' g ' = +, ' h ' = +, ' i ', ' + ', ' j ', ' + ', ' k ' = ', ' l ', ' = ', ' m ' +, ' n ' =& Gt ' O ', ' P ', ' + ', ' Q ', ' + ', ' r ' +, ' s ', ' + ', ' u ', ' + ', ' V ' =, ' W ' =& Gt The ' x ' = +, ' y ' = +, ' z ' = Panax notoginseng, ' A ', ' = ', ' B ' = ', ' C ' = +, ' D ' = ', ' E ' = ', ' F ' =& Gt The ' G ' = +, ' H ' and ' N ', ' I ' +, ' J ' =, ' K ', ' = ', ' L ', ' + ' Gt The ' P ' = +, ' Q ' = +, ' R ' = +, ' S ' = +, ' T ', ' + ', ' U ' and ' s ', ' V ' = ', ' W ' = ', ' X ' =& Gt , ' Y ' = +, ' Z ' = 63,);
  12. $result = 0;
  13. $len = strlen ($sixty _four);

  14. for ($n = 0; $n < $len; $n + +) {

  15. $result *= 64;
  16. $result + = $base _map[$sixty _four{$n}];
  17. }

  18. return $result;

  19. }

  20. $a =idate ("U");

  21. Var_dump (DEC2S4 ($a));
  22. Var_dump (S42dec (DEC2S4 ($a)));

Copy Code

Algorithm efficiency test:

    1. $strarr = Array ();

    2. $time 1 = microtime (true);
    3. for ($i = 0; $i < 10000; + + $i) {
    4. $str = Idate ("U") + $i;
    5. $strarr [] = "{$i}-> $str \ r \ n
      ";
    6. }
    7. $time 2 = Microtime (true);
    8. $time 3 = $time 2-$time 1;

    9. $time 1 = microtime (true);

    10. for ($i = 0; $i < 10000; + + $i) {
    11. $str = Dec2s4 (Idate ("U") + $i);
    12. $strarr [] = "{$i}-> $str \ r \ n
      ";
    13. }
    14. $time 2 = Microtime (true);
    15. echo "\ r \ n
      Run 10,000 times (seconds): ". ($time 2-$time 1-$time 3);

Copy Code

Test result: Algorithm 1:0.1687250137329 algorithm 2:0.044965028762817 conclusion: Algorithm 1 is less efficient, but it can convert the 16 MD5 generated into 64, which can be used to shorten the string in an environment where MD5 must be used.

Vi. the key point in summarizing the content of this article is the reduction of strings using the 10 binary conversion to 64 binary. For example, a 17-bit number generated using Fast_uuid, converted to 64 binary with only 7 characters;

The above is about the PHP upload image renaming all the content, I hope to help you.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.