Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How php uploads pictures and renames them

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how php uploads pictures to rename, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

First, applicable scenario: the uploaded image 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 copy 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.

Exampl

The copy code is as follows:

Output

The copy code is as follows:

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:

3recoveruniqid (): 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:

The copy code 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

The copy code is as follows:

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:

1ZZZZ UserID: 64-ary maximum "ZZZZ" to decimal equals "16777215", "ZZZ" to decimal maximum equals "262143"

2, 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

3, 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.

Fifth, decimal to 64-ary algorithm:

1, algorithm 1:

The copy code is as follows:

View Code

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))); 3,算法效率测试: 复制代码 代码如下: 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\ nRuntime (in seconds):". ($time2-$time1-$time3)

4, test results

Algorithm 1RV 0.1687250137329

Algorithm 2VR 0.044965028762817

5, 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.

Thank you for reading this article carefully. I hope the article "how to upload pictures and rename php" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report