Php Upload Image rename 6 solutions Research and Conclusion 1, applicable scenarios: unable to use the auto-increment number returned from the database, rename the uploaded image.
This is determined by the process of uploading images or files.
Generally, the image upload process is to upload the image to the server, rename it, and insert it to the database.
That is to say, the self-growth id that is easily obtained in the database cannot be used to rename the uploaded image to avoid duplication of file names,
The method of obtaining the maximum id and 1 from the database increases the number of database connections, which is not applicable to high concurrency and massive data volumes;
II. conventional solutions:
1, guid: 32 characters hexadecimal number.
Format: The GUID format is "xxxxxxxx-xxxx-xxxxxxxxxxxx", where each x is a 32-bit hexadecimal number in the range of 0-9 or a-f. For example, 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid GUID value.
Advantage: almost no repetition;
Disadvantage: it is too long to rename the uploaded image.
Usage:
/* Com_create_guid () is a function supported by php5. you can customize unsupported versions by yourself. */function guid () {if (function_exists ('com _ create_guid ') {return com_create_guid ();} else {mt_srand (double) microtime () * 10000 ); // optional for php 4.2.0 and up. echo (mt_rand (); $ charid = strtoupper (md5 (uniqid (rand (), true); $ hyphen = chr (45 ); // "-" $ uuid = chr (123 )//"{". substr ($ charid, 0, 8 ). $ hyphen. substr ($ charid, 8, 4 ). $ hyphen. substr ($ charid, 12, 4 ). $ hyphen. substr ($ charid, 16, 4 ). $ hyphen. substr ($ charid, 20, 12 ). chr (125); // "}" return $ uuid ;}}
2, MD5:
Like a guid, a 32-character hexadecimal number is output. The difference is that guids are randomly generated, and md5 needs to be generated based on the input data.
Example,
Output,
8b1a9953c4611296a827abf8c47804d7
Advantage: The output value can be controlled based on the input seed data. if the seed data is regular and does not repeat, md5 can be used to protect the data, resulting in great confusion.
Disadvantage: 32 characters are too long; non-repeated seed data must be provided;
Usage: high concurrency, with the second as the seed data, there will still be duplicates.
3, uniqid (): returns a string of 13 or 23 characters.
For our purpose, uniqid () is like the digest version of md5 (). In particular, we can use differential identifiers as the string prefix to reduce the chance of repeated names.
In extreme cases such as non-high concurrency, we recommend that you use this function to meet general requirements.
Detailed description,
Definition: The uniqid () function generates a unique ID based on the current time in microseconds.
Usage: uniqid (prefix, more_entropy)
Note: 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.
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 and the result contains data obfuscation, which can prevent the original data from being pushed back.
Disadvantages: Similar to md5, it is highly concurrent, and duplicate data occurs in seconds.
III. upgrade plan:
1, fast_uuid: return 17 digits.
It is a bit like the incomplete customization 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 what we actually need is only a value that can automatically increase.
After the start time is customized, in addition to reducing the length, it can also be confused.
/** The suffix_len parameter specifies the number of random numbers appended to the generated ID value. The default value is 3. * Thanks to the algorithms provided by "Ivan Tan | Tan Junqing DrinChing (at) Gmail.com. * @ Param int suffix_len * @ return string */function fast_uuid ($ suffix_len = 3 ){//! Start time for calculating the number of seeds $ being_timestamp = strtotime ('2017-3-21 '); $ time = explode ('', microtime ()); $ id = ($ time [1]-$ being_timestamp ). sprintf ('% 06u', substr ($ time [0], 2, 6); if ($ suffix_len> 0) {$ id. = substr (sprintf ('% 010u', mt_rand (), 0, $ suffix_len);} return $ id ;}
Output,
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 as follows,
IV. Final plan:
Train of Thought: userid + second + random number. The "userid + second" 10-to 64-to-64, reducing the number of digits;
Note:
1. userid: convert the 64-digit maximum value "ZZZZ" to "decimal" equal to "16777215", and "ZZZ" to a decimal maximum value equal to "262143";
2, second: set your own time start point.
$ Less = time ()-strtotime ('2017-4-21 '); convert to 64-digit "1SpRe", 5-bit
$ Less = time ()-strtotime ('1970-3-21 '); convert to 64-digit "_ jHY"; 4-bit
3. random number: use random (3) to generate a three-digit random number;
Final result:
4-digit userid + 4-digit second + 3-digit random number = 11-digit string. Although the results are similar to those of uniqid (), the robustness is improved.
5. Convert decimal to 64-digit algorithm:
1. Algorithm 1:
View Code
Const KeyCode = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz _ $ '; /*** convert a 64-digit numeric string into a 10-digit numeric string * @ param $ m string 64-digit numeric string * @ param $ len integer returns the string length, if the length is insufficient, fill it with 0. if 0 is left blank, * @ return string * @ author Mustang */function hex64to10 ($ m, $ len = 0) {$ m = (string) $ m; $ hex2 = ''; $ Code = KeyCode; for ($ I = 0, $ l = strlen ($ Code); $ I <$ l; $ I ++) {$ KeyCode [] = $ Code [$ I];} $ KeyCode = array_flip ($ KeyCode); for ($ I = 0, $ l = strlen ($ m); $ I <$ l; $ I ++) {$ one = $ m [$ I]; $ hex2. = str_pad (decbin ($ KeyCode [$ one]), 6, '0', STR_PAD_LEFT);} $ return = bindec ($ hex2); if ($ len) {$ clen = strlen ($ return); if ($ clen >=$ len) {return $ return;} else {return str_pad ($ return, $ len, '0 ', STR_PAD_LEFT) ;}} return $ return ;} /*** convert a 10-digit string to a 64-digit string * @ param $ m string a 10-digit string * @ param $ len integer returns the string length, if the length is not enough, fill it with 0. if 0 is left blank, * @ return string * @ author Mustang */function hex10to64 ($ m, $ len = 0) {$ KeyCode = KeyCode; $ hex2 = decbin ($ m); $ hex2 = str_rsplit ($ hex2, 6); $ hex64 = array (); foreach ($ hex2 as $ one) {$ t = bindec ($ one); $ hex64 [] = $ KeyCode [$ t];} $ return = preg_replace ('/^ 0 */','', implode ('', $ hex64); if ($ len) {$ clen = strlen ($ return); if ($ clen >=$ len) {return $ return ;} else {return str_pad ($ return, $ len, '0', STR_PAD_LEFT) ;}} return $ return ;} /*** convert a hexadecimal numeric string to a 64-hexadecimal numeric string * @ param $ m string a hexadecimal numeric string * @ param $ len integer returns the string length, if the length is not enough, fill it with 0. if 0 is left blank, * @ return string * @ author Mustang */function hex16to64 ($ m, $ len = 0) {$ KeyCode = KeyCode; $ hex2 = array (); for ($ I = 0, $ j = strlen ($ m); $ I <$ j; ++ $ I) {$ hex2 [] = str_pad (base_convert ($ m [$ I], 16, 2), 4, '0', STR_PAD_LEFT);} $ hex2 = implode ('', $ hex2); $ hex2 = str_rsplit ($ hex2, 6); foreach ($ hex2 as $ one) {$ hex64 [] = $ KeyCode [bindec ($ one)];} $ return = preg_replace ('/^ 0 */', '', implode ('', $ hex64); if ($ len) {$ clen = strlen ($ return); if ($ clen >=$ len) {return $ return;} else {return str_pad ($ return, $ len, '0 ', STR_PAD_LEFT) ;}} return $ return;}/*** functions are similar to the native PHP function str_split, only count and cut from the tail * @ param $ str string the string to be cut * @ param $ len integer the length of each string * @ return array * @ author Mustang */function str_rsplit ($ str, $ len = 1) {if ($ str = null | $ str = false | $ str = '') return false; $ strlen = strlen ($ str); if ($ strlen <= $ len) return array ($ str); $ headlen = $ strlen % $ len; if ($ headlen = 0) {return str_split ($ str, $ len);} $ return = array (substr ($ str, 0, $ headlen )); return array_merge ($ return, str_split (substr ($ str, $ headlen), $ len) ;}$ a = idate ("U"); echo "\ r \ n
E: ". hex10to64 ($ a); echo" \ r \ n
E: ". hex64to10 (hex10to64 ($ ));
2. Algorithm 2:
View Code
function dec2s4($dec) { $base = '0123456789_$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $result = ''; do { $result = $base[$dec % 64] . $result; $dec = intval($dec / 64); } while ($dec != 0); return $result; } function s42dec($sixty_four) { $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, ); $result = 0; $len = strlen($sixty_four); for ($n = 0; $n < $len; $n++) { $result *= 64; $result += $base_map[$sixty_four{$n}]; } return $result; } $a=idate("U");var_dump(dec2s4($a)); var_dump(s42dec(dec2s4($a)));
3. algorithm efficiency test:
View Code
$ Strarr = array (); $ time1 = microtime (true); for ($ I = 0; $ I <10000; ++ $ I) {$ str = idate ("U") + $ I; $ strarr [] = "{$ I}-> $ str \ r \ n
";}$ Time2 = microtime (true); $ time3 = $ time2-$ time1; $ time1 = microtime (true); for ($ I = 0; $ I <10000; + + $ I) {$ str = dec2s4 (idate ("U") + $ I); $ strarr [] = "{$ I}-> $ str \ r \ n
";}$ Time2 = microtime (true); echo" \ r \ n
10000 running times (seconds): ". ($ time2-$ time1-$ time3 );
4. test results
Algorithm 1687250137329
Algorithm. 044965028762817
5. 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.
VI. conclusion,
This article involves several methods that can be used to rename an uploaded image. The key point is to use a 10-in-one conversion to a 64-in-one 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;
You can use it flexibly based on your own situation. I hope it will help you.
References:
1, GUID Baidu Encyclopedia: http://baike.baidu.com/view/185358.htm
2, com_create_guid () Official Guide: http://www.php.net/manual/zh/function.com-create-guid.php
3, MD5 () function description: http://www.w3school.com.cn/php/func_string_md5.asp
4, time () function description: http://www.w3school.com.cn/php/func_date_time.asp
5, uniqid () function description: http://www.w3school.com.cn/php/func_misc_uniqid.asp