In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you what php upload picture renaming methods, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Applicable scenarios:
The uploaded picture cannot be renamed using the self-growing number returned from the database.
This is determined by the process of uploading pictures or files.
The general process of uploading a picture is to upload the picture to the server, rename it, and insert it into the database.
In other words, the self-growing id, which is very easy to obtain in the database, cannot be used to rename uploaded images to avoid duplicate file names.
However, the method of obtaining the maximum id from the database plus 1 increases the number of database connections, which is not suitable for situations with high concurrency and large amount of data.
Second, the conventional scheme:
1 guide guide 32 characters hexadecimal number.
Format: the format of GUID is "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where each x is a 32-bit hexadecimal number in the range of 0-9 or Amurf. For example, 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid GUID value.
Advantages: almost no repetition
Cons: it's still too long to rename the uploaded image.
Usage:
The code is as follows:
/ *
Com_create_guid () is a feature supported by the php5 version. For unsupported versions, you can define it 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 guid, it outputs 32-character hexadecimal numbers, except that guid is randomly generated and md5 needs to be generated based on the input data.
Example:
The copy code is as follows:
Output
8b1a9953c4611296a827abf8c47804d7
Advantages: the output value can be controlled according to the input seed data. If the seed data is regularly non-repetitive, the data can be protected through md5, resulting in great confusion.
Disadvantages: 32-bit characters are too long; non-repetitive seed data is required
Usage: high concurrency, with seconds as seed data, there will still be repetition.
The copy code is as follows:
3dint uniqid (): returns a 13-bit or 23-bit string
For our purposes, uniqid () looks like an improved version of md5 (), especially since we can use differential identifiers as string prefixes to reduce the chance of repeated naming.
For extreme cases such as non-high concurrency, it is recommended to use this function to meet general needs.
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 add a prefix to the output string. For example, when the more_entropy parameter is true, a 23-bit string will be output.
The copy code is as follows:
The output is as follows:
String (13) "51734aa562254" string (14) "a51734aa562257"
Advantages: 13-bit string length, is an acceptable file naming length; you can add a prefix, the result contains data confusion, can avoid extrapolating the original data.
Disadvantages: similar to md5, high concurrency, with seconds as the seed data, there will still be repetition.
III. Upgraded version of the scheme:
1Fastener uuid: returns 17 digits
A bit like the incomplete customized version of uniqid (), the concept of "seed count start time" in this function is enlightening.
The default time used in time () and uniqid () is calculated from 1970 and has a length of ten digits (1366512439). Using "seed start time" can reduce this value, because all we really need is a number that can automatically grow.
After the start time is customized, in addition to reducing the length, it can also play a confusing role.
The copy code is as follows:
/ *
* the parameter suffix_len specifies how many random digits to append to the generated ID value. The default value is 3.
* thanks for the algorithm provided by "Ivan Tan | Tan Junqing DrinChing (at) Gmail.com".
* @ param int suffix_len
* @ return string
, /
Function fast_uuid ($suffix_len=3) {
/ /! The start time of calculating the number of seeds
$being_timestamp = strtotime ('2013-3-21')
$time = explode ('', microtime ())
Id = ($time [1]-$being_timestamp). Sprintf ('upland, substr ($time [0], 2, 6))
If ($suffix_len > 0)
{
$id. = substr (sprintf ('0uqi, mt_rand ()), 0, $suffix_len)
}
Return $id
}
Output
29832412631099013
2Query time () + random number:
The use of random numbers has appeared in the above example to solve multiple requests that occur in one second. Two functions are provided as follows
The copy code is as follows:
Fourth, the final plan:
Idea: userid+ seconds + random numbers. Among them, "userid+ second" is converted from 10 to 64, which reduces the number of bits.
Description:
Userid: 64 base maximum "ZZZZ" to decimal equals "16777215", "ZZZ" to decimal maximum equals "262143"
Seconds: set your own time starting point.
$less=time ()-strtotime ('2012-4-21'); converted to 64-ary "1SpRe", 5 bits
$less=time ()-strtotime ('2013-3-21'); converted to 64-ary "_ jHY"; 4 bits
Random numbers: generate 3-digit random numbers using random (3)
End result:
4-bit userid+4 bit second + 3-bit random number = 11-bit string. Although the result looks similar to that of uniqid (), the robustness is improved.
5. Decimal to 64-ary algorithm:
Algorithm 1:
The copy code is as follows:
Const KeyCode = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
/ * *
* convert a 64-ary number string to a 10-ary number string
* @ param $m string 64 digit string
* @ param $len integer returns the string length. If the length is not enough to be filled with 0, 0 is not filled.
* @ 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, '0mm, STR_PAD_LEFT)
}
}
Return $return
}
/ * *
* convert a decimal number string to a 64-ary numeric string
* @ param $m string 10 digit string
* @ param $len integer returns the string length. If the length is not enough to be filled with 0, 0 is not filled.
* @ 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 implode (', $hex64))
If ($len) {
$clen = strlen ($return)
If ($clen > = $len) {
Return $return
}
Else {
Return str_pad ($return, $len, '0mm, STR_PAD_LEFT)
}
}
Return $return
}
/ * *
* convert a hexadecimal numeric string to a 64 numeric string
* @ param $m string hexadecimal numeric string
* @ param $len integer returns the string length. If the length is not enough to be filled with 0, 0 is not filled.
* @ 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, '0mm, STR_PAD_LEFT)
}
}
Return $return
}
/ * *
* the function is similar to the PHP native function str_split, except that it starts counting and cutting from the tail.
* @ param $str string 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 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))); 算法效率测试: 复制代码 代码如下: $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\ nRuntime (in seconds):". ($time2-$time1-$time3)
Test result
Algorithm 1RV 0.1687250137329
Algorithm 2VR 0.044965028762817
Conclusion: although algorithm 1 is less efficient, it can convert the hexadecimal generated by md5 into 64, and can be used to shorten strings when md5 must be used.
The above is all the contents of the article "what are the renaming methods for uploading pictures on php?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.