Research and summary of six kinds of PHP image uploading and renaming scheme
First, the application scenario
Unable to rename the uploaded image using the self-growth numbers returned from the database.
This is determined by the process of picture or file upload. General image upload process is to upload images to the server, after renaming, inserted into the database.
?
That is, in the database is very easy to obtain the self-growth ID, can not be used to rename the uploaded image, to avoid duplication of file names, and the use of the database to obtain the maximum ID plus 1, the number of database connections increased, not for high concurrency and large data volume situation.
?
II. Conventional programmes
1, guid:32 character hexadecimal number
Format: The format of the GUID is "xxxxxxxx-xxxx-xxxx-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.
Advantages: almost no repetition;
Cons: Renaming an uploaded image is too long.
Usage:
/* Com_create_guid () is a feature supported by the PHP5 version, and for unsupported versions, you can define your own */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;//"-" $uuid = chr (123)//"{" . substr ($charid, 0, 8). $hyphen . substr ($charid, 8, 4). $ Hyphen . substr ($charid, 4). $hyphen . substr ($charid, 4). $hyphen . substr ($charid, 20,12) . chr;//"}" return $uuid; }}
?
2, MD5
As with GUIDs, the 32-character hexadecimal number is output, except that the GUID is randomly generated and MD5 needs to be generated from the input data.
Example:
?
Output:
8b1a9953c4611296a827abf8c47804d7
Advantages: Can be based on the input of the seed data to control the output value, if the seed data is regular and non-repeating, through the MD5 can protect the data, resulting in a great confusion;
Cons: 32 characters are too long, need to provide non-duplicated seed data;
Usage: High concurrency, with seconds as the seed data, will still appear repeated.
?
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.
Detailed Description:
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, which, for example, will output a 23-bit string if the More_entropy parameter is true.
?
The output is:
String (51734aa562254) string "a51734aa562257"
?
Pros: 13-bit string length, is acceptable file name length, you can add a prefix, the result contains data confusion, can avoid the inverse 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 plan
1. Fast_uuid: Returns 17 digits
Somewhat like the uniqid (), the concept of "seed number start Time", which appears in this function, is instructive.
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 what we actually need is just a 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.
/** parameter Suffix_len Specifies how many bits of random number are appended to the generated ID value, the default value is @param int suffix_len* @return string*/function fast_uuid ($suffix _len=3) {
//! Calculate the start time of the seed number $being _timestamp = strtotime (' 2013-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
The use of random numbers has occurred in the above example in order to resolve multiple requests that occur in one second. Two functions are provided as follows:
?
IV. Final programme
Idea: userid+ seconds + random number. where "userid+ seconds" 10 binary to 64, reducing the number of bits.
Description
1, userid:64 binary maximum "ZZZZ" Conversion to decimal equals "16777215", "ZZZ" is converted to a decimal maximum equal to "262143";
2, seconds: Set your own time starting point.
$less =time ()-strtotime (' 2012-4-21′) converted to 64 binary "1SpRe", 5-bit
$less =time ()-strtotime (' 2013-3-21′) converted to 64 binary "_jhy", 4-bit
3, random Number: Use Random (3) to generate 3-bit random numbers.
Final Result: 4-bit userid+4 bit seconds + 3-bit random number = 11-bit string. Although the results look similar to the uniqid (), the strength has improved.
?
Five, decimal to 64 binary algorithm
1. Algorithm 1
Const KEYCODE = ' 0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz_$ '; /** * Converts a 64-digit string into a 10-digit String * @param $m string 64 binary String * @param $len Integer Returns the length of the string, if the length is not sufficient with 0 padding, 0 is not populated * @return String */function Hex64to10 ($m, $len = 0) {$m = (string) $m; $hex 2 = "; $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]; $hex 2. = Str_pad (Decbin ($KeyCode [$one]), 6, ' 0 ', str_pad_left); } $return = Bindec ($hex 2); if ($len) {$clen = strlen ($return); if ($clen >= $len) {return $return; } else {return Str_pad ($return, $len, ' 0 ', str_pad_left); }} return $return; }/** * Converts a 10-binary numeric string into a 64-digit string * @param $m StRing 10 Binary Numeric String * @param $len Integer Returns the length of the string, if the length is not sufficient with 0 padding, 0 is not populated * @return String */function Hex10to64 ($m, $ Len = 0) {$KeyCode = KeyCode; $hex 2 = Decbin ($m); $hex 2 = Str_rsplit ($hex 2, 6); $hex = Array (); foreach ($hex 2 as $one) {$t = Bindec ($one); $hex 64[] = $KeyCode [$t]; } $return = Preg_replace ('/^0*/', ' ', implode (', $hex 64)); if ($len) {$clen = strlen ($return); if ($clen >= $len) {return $return; } else {return Str_pad ($return, $len, ' 0 ', str_pad_left); }} return $return; }/** * Converts a 16-digit string into a 64-digit String * @param $m string 16 binary String * @param $len Integer Returns the length of the string, if the length is not sufficient with 0 padding, 0 is not filled Charge * @return String */function Hex16to64 ($m, $len = 0) {$KeyCode = KeyCode; $hex 2 = array (); for ($i = 0, $j = strlen ($m), $i < $j, + + $i) {$hex 2[]= Str_pad (Base_convert ($m [$i], 2), 4, ' 0 ', str_pad_left); } $hex 2 = Implode (' ', $hex 2); $hex 2 = Str_rsplit ($hex 2, 6); foreach ($hex 2 as $one) {$hex 64[] = $KeyCode [Bindec ($one)]; } $return = Preg_replace ('/^0*/', ' ', implode (', $hex 64)); if ($len) {$clen = strlen ($return); if ($clen >= $len) {return $return; } else {return Str_pad ($return, $len, ' 0 ', str_pad_left); }} return $return; }/** * Function and PHP native function Str_split Close, just start counting cut from Tail * @param $str string required to be cut * @param $len length of each string of integers * @return Array */function Str_rsplit ($str, $len = 1) {if ($str = = NULL | | $str = = FALSE | | $str = = ") ret Urn 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 ($a));
?
2. Algorithm 2
function Dec2s4 ($dec) {$base = ' 0123456789_$abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz '; $result = "; do {$result = $base [$dec]. $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 ' and ' 7, ' 8 ' + 8, ' 9 ' and ' 9 ', ' _ ' and ' $ ', ' $ ' + one, ' a ' and ' = ', ' b ' = ' + ', ' c ' = 14 , ' d ' and ' K ', ' e ' = +, ' f ' = ' + ', ' g ' = +, ' h ' = +, ' i ' = +, ' j ', And, ' n ' +, ' o ' = +, ' p ' = +, ' q ' +, ' r ' +, ' s ' = ', ' t ', ' + ', ' u ' = +, ' V ' = , ' w ' = +, ' x ' = +, ' y ' = +, ' z ' = Notoginseng, ' A ', ' = ', ' B ' = ', ' C ' = +, ' D ' = ', ' E ' = 42, ' F ' = +, ' G ', ' = ', ' H ', ' and ', ' I ' = ', ' J ' = ', ' K ' = ', ' L ', ' + ', ' M ' = ', ' N ' =, ' O ' =& GtThe ' P ' = +, ' Q ' = +, ' R ' = +, ' S ' = +, ' T ', ' + ', ' U ' and ' s ', ' V ' = ', ' W ' = ', ' X ' = ' Y ' ' = ' +, ' 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
$strarr = Array (), $time 1 = Microtime (True), for ($i = 0; $i < 10000; + + $i) { $str = idate ("U") + $i; $strarr [] = "{$i}-> $str \ r \ n
"; } $time 2 = Microtime (true); $time 3 = $time 2-$time 1; $time 1 = microtime (true); for ($i = 0; $i < 10000; + + $i) { $str = dec2s4 (Idate ("U") + $i); $strarr [] = "{$i}-> $str \ r \ n
";} $time 2 = Microtime (true); echo "\ r \ n
Run 10,000 times (seconds): ". ($time 2-$time 1-$time 3);
?
4. Test results
Algorithm 1:0.1687250137329
Algorithm 2:0.044965028762817
?
5. Conclusion
Algorithm 1 is less efficient, but it can convert the 16 binary generated by MD5 into 64, which can be used to shorten strings in environments where MD5 must be used.
?
Vi. Summary
This article deals with several methods that can be used to rename an upload image, where the key point is to use a 10 binary conversion to 64 to reduce the string.
For example, a 17-bit number generated using Fast_uuid, converted to 64 binary with only 7 characters;
Specific use, can be used flexibly according to their own situation, I hope to help you.
Reprint Please specify: Program ape? Research and summary of six kinds of PHP image uploading and renaming scheme
?
?