Six methods for renaming uploaded images in php-php Tutorial

Source: Internet
Author: User
Tags idate
Six methods for renaming uploaded images in php

  1. /*
  2. Com_create_guid () is a function supported by php5. you can customize unsupported versions by yourself;
  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 (1, 125 );//"}"
  19. Return $ uuid;
  20. }
  21. }

2. MD5: The 32-character hexadecimal number is output like The guid. The difference is that the guid is generated randomly and the md5 value must be generated based on the input data. Example:

  1. $ Str = "Hello ";
  2. Echo md5 ($ str );
  3. ?>

Output result: 8b1a9953c4611296a827abf8c47804d7 advantage: the output value can be controlled based on the input seed data. if the seed data is regular and does not overlap, md5 can be used to protect the data, this produces great obfuscation. Disadvantage: 32-bit characters are too long; non-repeated seed data must be provided; usage: high concurrency. the seed data in seconds will still be duplicated. Example:

  1. /*
  2. * Used in combination with the time () function, the number of seconds from January 1, 1970 to the current time is used as the number of seeds.
  3. */
  4. $ Str = time ();
  5. Echo md5 ($ str );
  6. ?>

3, uniqid (): returns a 13 or 23-bit string. for our purpose, uniqid () is like an md5 () character version. in particular, we can use differential identifiers as the string prefix, this reduces the chances of duplicate names. In extreme cases such as non-high concurrency, we recommend that you 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 be used to add a prefix to the output string. for example, if the more_entropy parameter is true, a 23-bit string is output.

Example:

  1. Var_dump (uniqid ());
  2. Var_dump (uniqid (""));
  3. ?>

Output result: string (13) "51734aa562254" string (14) "a51734aa562257" advantage: 13-bit string length, which is an acceptable file name length. you can add a prefix, the result contains data obfuscation, which can avoid reverse pushing of the original data. Disadvantages: Similar to md5, it is highly concurrent, and duplicate data occurs in seconds.

3. upgrade solution: 1. fast_uuid: return 17 digits, which is a bit like the incomplete customized version of uniqid (). The concept of "start time of seeds" in this function is very enlightening. The default time used in time () and uniqid () is calculated from January 1, 1970, with a length of 10 (1366512439). This value can be reduced using the "seed start time. Because, from the perspective of actual needs, it is just a value that can automatically increase. After the start time is customized, in addition to reducing the length, it can also be confused.

Example:

  1. /*

  2. * The suffix_len parameter specifies the number of random numbers appended to the generated ID value. The default value is 3.
  3. * Thanks to the algorithms provided by "Ivan Tan | Tan Junqing DrinChing (at) Gmail.com.
  4. * @ Param int suffix_len
  5. * @ Return string
  6. */
  7. Function fast_uuid ($ suffix_len = 3 ){
  8. //! Start time for calculating the number of seeds
  9. $ Being_timestamp = strtotime ('2017-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. }

Output result: 29832412631099013

2, time () + random number: in the preceding example, a random number is used to solve multiple requests in one second. Two functions are provided:

  1. Function random ($ length ){
  2. $ Hash = '';
  3. $ Chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxy ';
  4. $ Max = strlen ($ chars)-1;
  5. PHP_VERSION <'4. 100' & mt_srand (double) microtime () * 2.0 );
  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 (), 16, $ numeric? 10: 35 );
  14. $ Seed = $ numeric? (Str_replace ('0', '', $ seed). '000000'): ($ 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. ?>

IV. Final Solution: Train of Thought: userid + second + random number. "Userid + second" 10-digit to 64-digit, reducing the number of digits; description: userid: 64-digit maximum value "ZZZZ" to decimal equal to "16777215", "ZZZ" is converted to a decimal maximum value equal to "262143"; second: set your own time start point.

  1. Const KeyCode = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz _ $ ';

  2. /**
  3. * Convert a 64-digit numeric string to a 10-digit numeric string.
  4. * @ Param $ m string 64-digit numeric string
  5. * @ Param $ len integer returns the length of the string. if the length is not enough, fill the string with 0. if the length is not enough, do not fill the string with 0.
  6. * @ Return string
  7. * @ Author Mustang
  8. */
  9. Function hex64to10 ($ m, $ len = 0 ){
  10. $ M = (string) $ m;
  11. $ Hex2 = '';
  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. $ Hex2. = str_pad (decbin ($ KeyCode [$ one]), 6, '0', STR_PAD_LEFT );
  20. }
  21. $ Return = bindec ($ hex2 );

  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. * Convert a 10-digit numeric string to a 64-digit numeric string.
  35. * @ Param $ m string a 10-digit numeric string
  36. * @ Param $ len integer returns the length of the string. if the length is not enough, fill the string with 0. if the length is not enough, do not fill the string with 0.
  37. * @ Return string
  38. * @ Author Mustang
  39. */
  40. Function hex10to64 ($ m, $ len = 0 ){
  41. $ KeyCode = KeyCode;
  42. $ Hex2 = decbin ($ m );
  43. $ Hex2 = str_rsplit ($ hex2, 6 );
  44. $ Hex64 = array ();
  45. Foreach ($ hex2 as $ one ){
  46. $ T = bindec ($ one );
  47. $ Hex64 [] = $ KeyCode [$ t];
  48. }
  49. $ Return = preg_replace ('/^ 0 */', '', implode ('', $ hex64 ));
  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. * Convert a hexadecimal numeric string to a 64-digit numeric string.
  63. * @ Param $ m string hexadecimal numeric string
  64. * @ Param $ len integer returns the length of the string. if the length is not enough, fill the string with 0. if the length is not enough, do not fill the string with 0.
  65. * @ Return string
  66. * @ Author Mustang
  67. */
  68. Function hex16to64 ($ m, $ len = 0 ){
  69. $ KeyCode = KeyCode;
  70. $ Hex2 = array ();
  71. For ($ I = 0, $ j = strlen ($ m); $ I <$ j; ++ $ I ){
  72. $ Hex2 [] = str_pad (base_convert ($ m [$ I], 16, 2), 4, '0', STR_PAD_LEFT );
  73. }
  74. $ Hex2 = implode ('', $ hex2 );
  75. $ Hex2 = str_rsplit ($ hex2, 6 );
  76. Foreach ($ hex2 as $ one ){
  77. $ Hex64 [] = $ KeyCode [bindec ($ one)];
  78. }
  79. $ Return = preg_replace ('/^ 0 */', '', implode ('', $ hex64 ));
  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. * The function is similar to the PHP native function str_split. it only starts counting and cutting from the end.
  93. * @ Param $ str string the string to be cut
  94. * @ Param $ len integer the length of each string segment
  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 ($ );
  111. Echo "\ r \ n
    E: ". hex64to10 (hex10to64 ($ ));

Algorithm 2:

  1. Function dec2s4 ($ dec ){

  2. $ Base = '1970 _ $ 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 <$ 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 ($ ));
  22. Var_dump (s42dec (dec2s4 ($ )));

Algorithm efficiency test:

  1. $ Strarr = array ();

  2. $ Time1 = microtime (true );
  3. For ($ I = 0; $ I <10000; ++ $ I ){
  4. $ Str = idate ("U") + $ I;
  5. $ Strarr [] = "{$ I}-> $ str \ r \ n
    ";
  6. }
  7. $ Time2 = microtime (true );
  8. $ Time3 = $ time2-$ time1;

  9. $ Time1 = microtime (true );

  10. For ($ I = 0; $ I <10000; ++ $ I ){
  11. $ Str = dec2s4 (idate ("U") + $ I );
  12. $ Strarr [] = "{$ I}-> $ str \ r \ n
    ";
  13. }
  14. $ Time2 = microtime (true );
  15. Echo "\ r \ n
    10000 running times (seconds): ". ($ time2-$ time1-$ time3 );

Test results: algorithm 1:0. 1687250137329 algorithm. 044965028762817 conclusion: Although Algorithm 1 is less efficient, it can convert the hexadecimal format generated by md5 to 64, which can shorten the string in an environment where md5 must be used.

6. to sum up the content of this article, the key point is to use a 10-to-64 conversion to reduce the string. For example, a 17-digit number generated using fast_uuid is converted to a 64-digit number with only 7 characters;

The above is all about renaming uploaded images in php. I hope it will be helpful to you.

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.