First, the applicable scene:
You cannot rename an uploaded image by using the self growth number returned from the database.
This is the picture or file upload process decision.
The general image upload process is, first upload pictures to the server, after renaming, insert into the database.
That is, the self growth ID, which is very easy to obtain in the database, cannot be used to rename the uploaded image to avoid duplication of file names.
The method of obtaining the maximum ID plus 1 from the database increases the number of database connections and does not apply to high concurrency and large data volumes.
Second, the general programme:
1,guid:32 character hexadecimal number.
Format: The GUID is formatted as "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where each x is a 32-bit hexadecimal number in the range 0-9 or a-f. For example: 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid GUID value.
Advantages: almost no repetition;
Disadvantage: For the upload of the image renamed, or too long.
Usage:
Copy Code code as follows:
/*
Com_create_guid () is a feature supported by the PHP5 version and can be defined for unsupported versions.
*/
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, 4). $hyphen
. substr ($charid, 4). $hyphen
. substr ($charid, 20,12)
. chr (125);//"}"
return $uuid;
}
}
2,MD5:
As with the GUID, 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:
Copy Code code as follows:
<?php
$str = "Hello";
echo MD5 ($STR);
?>
Output
8b1a9953c4611296a827abf8c47804d7
Advantages: According to the input of the seed data to control the output of the value, if the seed data is regular and repeat, through the MD5 can protect the data, resulting in a great confusion.
Disadvantage: 32-bit characters are too long, you need to provide the seed data without duplication;
Usage: High concurrency, seed data in seconds, and there will still be duplication.
Copy Code code as follows:
<?php
/*
* Use the time () function to count the number of seconds in the current 1970 years.
*/
$str =time ();
echo MD5 ($STR);
?>
3,uniqid (): Returns a 13-or 23-bit string
For our purposes, uniqid () is an improved version of MD5 (), especially if we can use the differential identifier as a string prefix to reduce the chance of duplicate naming.
This function is recommended for extreme situations such as non-high concurrency, which can already satisfy 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, for example, when the more_entropy argument is true, the 23-bit string is output.
Copy Code code as follows:
<?php
Var_dump (Uniqid ());
Var_dump (Uniqid ("a"));
?>
The output results are:
String (51734aa562254) string "a51734aa562257"
Advantages: 13-bit string length, acceptable file name length, can be added prefix, the result contains data obfuscation, to avoid the reverse of the original data.
Disadvantages: Similar to MD5, high concurrency, with the second as seed data, there will still be duplicate phenomena.
third, upgrade version of the program:
1,fast_uuid: Returning 17 digits
Somewhat like Uniqid (), the concept of "seed count start time" In this function is illuminating.
Time () and uniqid () are used by default since 1970, with a length of 10 bits (1366512439) and a "seed number start time" to reduce this value, because what we actually need is a number that can automatically grow.
Since the start time customization, in addition to reducing the length, but also can play a confusing role.
Copy Code code as follows:
/*
* Parameter Suffix_len Specifies the number of random digits appended to the generated ID value, and the default value is 3.
* Thanks to the algorithm provided by Ivan tan| Tan Junqing drinching (at) gmail.com.
* @param int Suffix_len
* @return String
*/
function Fast_uuid ($suffix _len=3) {
//! Calculate the start time of the seed count
$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 already occurred in the previous example to resolve multiple requests that occur in one second. Provides two functions as follows,
Copy Code code as follows:
<?php
function Random ($length) {
$hash = ';
$chars = ' abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ';
$max = strlen ($chars)-1;
Php_version < ' 4.2.0 ' && mt_srand (double) microtime () * 1000000);
for ($i = 0; $i < $length; $i + +) {
$hash. = $chars [Mt_rand (0, $max)];
}
return $hash;
}
function Random2 ($length, $numeric = 0) {
Php_version < ' 4.2.0 '? Mt_srand (Double) microtime () * 1000000): Mt_srand ();
$seed = Base_convert (MD5 (Print_r ($_server, 1). Microtime ()), $numeric? 10:35);
$seed = $numeric? (Str_replace (' 0 ', ', $seed). " 012340567890 '): ($seed. ' ZZ '. Strtoupper ($seed));
$hash = ';
$max = strlen ($seed)-1;
for ($i = 0; $i < $length; $i + +) {
$hash. = $seed [Mt_rand (0, $max)];
}
return $hash;
}
?>
Iv. Final Programme:
Train of thought: userid+ second + random number. where "userid+ second" 10 into the 64 to reduce the number of digits;
Description
The userid:64 maximum "ZZZZ" converts to decimal equals "16777215", and "ZZZ" converts to decimal maximum equal to "262143";
Seconds: Set your own time starting point.
$less =time ()-strtotime (' 2012-4-21′); Convert to 64-in "1SpRe", 5-bit
$less =time ()-strtotime (' 2013-3-21′); Convert to 64-in "_jhy"; 4-bit
Random number: Using 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 result looks similar to the uniqid (), the strength increases.
five or 10-in-turn 64-in-Process algorithm:
Algorithm 1:
Copy Code code as follows:
Const KEYCODE = ' 0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz_$ ';
/**
* Convert a 64-digit string into a 10-digit string
* @param $m string 64-digit string
* @param $len Integer Returns the length of the string, if the length is not enough to fill with 0, 0 is not filled
* @return String
* @author Mustang
*/
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;
}
/**
* Convert a 10-digit string into a 64-digit string
* @param $m string 10-digit string
* @param $len Integer Returns the length of the string, if the length is not enough to fill with 0, 0 is not filled
* @return String
* @author Mustang
*/
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;
}
/**
* Convert a 16-digit string into a 64-digit string
* @param $m string 16-digit string
* @param $len Integer Returns the length of the string, if the length is not enough to fill with 0, 0 is not filled
* @return String
* @author Mustang
*/
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 from the tail cutting
* @param $str string to be cut
* @param $len integer length of each segment
* @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<br/>e:". Hex10to64 ($a);
echo "\r\n<br/>e:". Hex64to10 (hex10to64 ($a));
Algorithm 2:
Copy Code code as follows:
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,&NBSP;&NB sp; ' 3 ' => 3, ' 4 ' => 4, ' 5 ' => 5, ' 6 ' => 6, & nbsp; ' 7 ' => 7, ' 8 ' => 8, ' 9 ' => 9, ' _ ' => 10,&nbs p; ' $ ' => 11, ' a ' => 12, ' B ' => 13, ' C ' => 1 4, ' d ' => 15, ' e ' => 16, ' f ' => ' g ' => 18, ' h ' => 19, ' i ' => 20, ' j ' => 21, & nbsp ' k ' => 22, ' l ' => 23, ' m ' => 24, ' n ' => 25, ' o ' => 26, ' P ' => 27, ' q ' => 28, ' r ' => 29, ' s ' => 30, ' t ' => 31, ' u ' => 32, ' V ' => ' W ' => 34, ' x ' => 35, ' y ' => 36, ' z ' => nbsp ' 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 ' =& Gt 53, ' Q ' => 54, ' R ' => 55, ' S ' => 56, ' T ' => 57, ' U ' => 58, ' V ' => 59, ' W ' => 60, & nbsp ' 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)));
Algorithm efficiency test:
Copy Code code as follows:
$strarr = Array ();
$time 1 = microtime (true);
for ($i = 0; $i < 10000 + + $i) {
$str = Idate ("U") + $i;
$strarr [] = "{$i}-> $str \r\n<br>";
}
$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<br>";
}
$time 2 = Microtime (true);
echo "\r\n<br/> Run 10,000 times (sec):". ($time 2-$time 1-$time 3);
Test results
Algorithm 1:0.1687250137329
Algorithm 2:0.044965028762817
Conclusion: Although the algorithm 1 is less efficient, it can convert the MD5 generated 16 into 64, and can shorten the string in an environment that must use MD5.
Vi. Summary
This article deals with several methods of uploading image renaming, in which the key point is to use 10 to convert to 64 to reduce the string.
For example, using the 17-digit number generated by Fast_uuid, the conversion to the 64 binary only has 7-bit characters;
Specific use, can be used flexibly according to their own situation, I hope to help you.