Home >Backend Development >PHP Tutorial >Detailed introduction to 6 solutions to renaming uploaded images based on PHP_PHP Tutorial
1. Applicable scenarios: You cannot use the self-increasing number returned from the database to rename uploaded images.
This is determined by the process of uploading images or files.
The general image upload process is to first upload the image to the server, rename it, and then insert it into the database.
That is to say, the self-increasing ID that is very easy to obtain in the database cannot be used to rename uploaded pictures to avoid duplication of file names.
Instead, the maximum ID plus 1 is obtained from the database. , increases the number of database connections, and is not suitable for situations with high concurrency and huge data volume;
2. Conventional plan:
1, guid: 32-character hexadecimal number.
Format: The GUID is in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where each x is a 32-digit 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;
Disadvantages: It is still too long for renaming uploaded pictures.
Usage:
3. Upgraded version plan:
1, fast_uuid: Returns a 17-digit number.
A bit like an incompletely customized version of uniqid(), the concept of "seed number starting time" that appears in this function is very enlightening.
The default time used in time() and uniqid() is calculated from 1970, and the length is ten digits (1366512439). Using the "seed number start time" can reduce this value, because we actually need Yes, it is just a value that can grow automatically.
After customizing the starting time, in addition to reducing the length, it can also play a role in confusion.
Description:
1, userid: The maximum value of "ZZZZ" converted into decimal is equal to "16777215", and the maximum value of "ZZZ" converted into decimal is equal to "262143";
2, seconds: set yourself the starting point of time.
$less=time()-strtotime('2012-4-21'); Convert to hexadecimal "1SpRe", 5 digits
$less=time()-strtotime('2013-3-21 '); Convert to hexadecimal "_jHY"; 4 digits
3, random number: use random(3) to generate a 3-digit random number;
4-digit userid + 4-digit second + 3-digit random number = 11-digit string. Although the results look similar to uniqid(), the robustness is improved.
Five, decimal to hexadecimal conversion algorithm:
1, Algorithm 1:const KeyCode = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$';
/**
* Convert a 64-digit string to a decimal string
* @param $m string 64-digit string
* @param $len integer Returns the length of the string , if the length is not enough, fill it with 0, 0 means no padding
* @return string
* @author 马野
*/
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 the decimal numeric string to a 64-digit numeric string
* @param $m string Decimal numeric string
* @param $len integer Returns the string length , if the length is not enough, fill it with 0, 0 means no padding
* @return string
* @author 马野
*/
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 hexadecimal number string to hexadecimal number string
* @param $m string Hexadecimal number string
* @param $len integer Returns the string length , if the length is not enough, fill it with 0, 0 means no padding
* @return string
* @author 马野
*/
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;
}
/**
* The function is similar to the PHP native function str_split, except that the cutting starts from the end
* @param $str string The string to be cut
* @param $len integer The length of each string
* @return array
* @author 马野
*/
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 "rn
e:" . hex10to64($a);
echo "rn
e:" . hex64to10(hex10to64($a));
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)));
$strarr = array();
$time1 = microtime(true);
for($i = 0; $i < 10000; ++$i) {
$str = idate("U")+$i;
$strarr[] = "{$i}->$strrn
";
}
$time2 = microtime(true);
$time3 = $time2 - $time1;
$time1 = microtime(true);
for($i = 0; $i < 10000; ++$i) {
$str = dec2s4(idate("U")+$i);
$strarr[] = "{$i}->$strrn
";
}
$time2 = microtime(true);
echo "rn
运行10000次用时(秒):" . ($time2 - $time1 - $time3);
6. Summary
This article involves several methods that may be used to rename uploaded images. The key point is to use decimal to hexadecimal to reduce the string.
For example, the 17-digit number generated by fast_uuid is converted into hexadecimal with only 7 characters;
The specific use can be used flexibly according to your own situation. I hope it will be helpful to everyone.
Reference:
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