6 Kinds of PHP Upload image Rename method instance _php instance

Source: Internet
Author: User
Tags chr current time file upload hash idate sprintf unique id uuid
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:



&lt;?php


function Random ($length) {


$hash = ';


$chars = ' abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ';


$max = strlen ($chars)-1;


Php_version &lt; ' 4.2.0 ' &amp;&amp; mt_srand (double) microtime () * 1000000);


for ($i = 0; $i &lt; $length; $i + +) {


$hash. = $chars [Mt_rand (0, $max)];


}


return $hash;


}


function Random2 ($length, $numeric = 0) {


Php_version &lt; ' 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 &lt; $length; $i + +) {


$hash. = $seed [Mt_rand (0, $max)];


}


return $hash;


}


?&gt;





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 &lt; $l; $i + +) {


$KeyCode [] = $Code [$i];


}


$KeyCode = Array_flip ($KeyCode);





for ($i = 0, $l = strlen ($m); $i &lt; $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 &gt;= $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 &gt;= $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 &lt; $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 &gt;= $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 &lt;= $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&lt;br/&gt;e:". Hex10to64 ($a);


echo "\r\n&lt;br/&gt;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) {


&nbsp;&nbsp;&nbsp; $base _map = Array (' 0 ' =&gt; 0,&nbsp;&nbsp;&nbsp; ' 1 ' =&gt; 1,&nbsp;&nbsp;&nbsp; ' 2 ' =&gt; 2,&NBSP;&NB sp;&nbsp; ' 3 ' =&gt; 3,&nbsp;&nbsp;&nbsp; ' 4 ' =&gt; 4,&nbsp;&nbsp;&nbsp; ' 5 ' =&gt; 5,&nbsp;&nbsp;&nbsp; ' 6 ' =&gt; 6,&nbsp;& nbsp;&nbsp; ' 7 ' =&gt; 7,&nbsp;&nbsp;&nbsp; ' 8 ' =&gt; 8,&nbsp;&nbsp;&nbsp; ' 9 ' =&gt; 9,&nbsp;&nbsp;&nbsp; ' _ ' =&gt; 10,&nbs p;&nbsp;&nbsp; ' $ ' =&gt; 11,&nbsp;&nbsp;&nbsp; ' a ' =&gt; 12,&nbsp;&nbsp;&nbsp; ' B ' =&gt; 13,&nbsp;&nbsp;&nbsp; ' C ' =&gt; 1 4,&nbsp;&nbsp;&nbsp; ' d ' =&gt; 15,&nbsp;&nbsp;&nbsp; ' e ' =&gt; 16,&nbsp;&nbsp;&nbsp; ' f ' =&gt; ' g ' =&gt; 18,&nbsp;&nbsp;&nbsp; ' h ' =&gt; 19,&nbsp;&nbsp;&nbsp; ' i ' =&gt; 20,&nbsp;&nbsp;&nbsp; ' j ' =&gt; 21,&nbsp;&nbsp;& nbsp ' k ' =&gt; 22,&nbsp;&nbsp;&nbsp; ' l ' =&gt; 23,&nbsp;&nbsp;&nbsp; ' m ' =&gt; 24,&nbsp;&nbsp;&nbsp; ' n ' =&gt; 25,&nbsp;&nbsp; &nbsp; ' o ' =&gt; 26,&nbsp;&nbsp;&nbsp; ' P ' =&gt; 27,&nbsp;&nbsp;&nbsp; ' q ' =&gt; 28,&nbsp;&nbsp;&nbsp; ' r ' =&gt; 29,&nbsp; &nbsp;&nbsp; ' s ' =&gt; 30,&nbsp;&nbsp;&nbsp; ' t ' =&gt; 31,&nbsp;&nbsp;&nbsp; ' u ' =&gt; 32,&nbsp;&nbsp;&nbsp; ' V ' =&gt; ' W ' =&gt; 34,&nbsp;&nbsp;&nbsp; ' x ' =&gt; 35,&nbsp;&nbsp;&nbsp; ' y ' =&gt; 36,&nbsp;&nbsp;&nbsp; ' z ' =&gt; nbsp ' A ' =&gt; 38,&nbsp;&nbsp;&nbsp; ' B ' =&gt; 39,&nbsp;&nbsp;&nbsp; ' C ' =&gt; 40,&nbsp;&nbsp;&nbsp; ' D ' =&gt; 41,&nbsp;&nbsp; &nbsp; ' E ' =&gt; 42,&nbsp;&nbsp;&nbsp; ' F ' =&gt; 43,&nbsp;&nbsp;&nbsp; ' G ' =&gt; 44,&nbsp;&nbsp;&nbsp; ' H ' =&gt; 45,&nbsp; &nbsp;&nbsp; ' I ' =&gt; 46,&nbsp;&nbsp;&nbsp; ' J ' =&gt; 47,&nbsp;&nbsp;&nbsp; ' K ' =&gt; 48,&nbsp;&nbsp;&nbsp; ' L ' =&gt; 49, &nbsp;&nbsp;&nbsp; ' M ' =&gt; 50,&nbsp;&nbsp;&nbsp; ' N ' =&gt; 51,&nbsp;&nbsp;&nbsp; ' O ' =&gt; 52,&nbsp;&nbsp;&nbsp; ' P ' =& Gt 53,&nbsp;&nbsp;&nbsp; ' Q ' =&gt; 54,&nbsp;&nbsp;&nbsp; ' R ' =&gt; 55,&nbsp;&nbsp;&nbsp; ' S ' =&gt; 56,&nbsp;&nbsp;&nbsp; ' T ' =&gt; 57,&nbsp;&nbsp;&nbsp; ' U ' =&gt; 58,&nbsp;&nbsp;&nbsp; ' V ' =&gt; 59,&nbsp;&nbsp;&nbsp; ' W ' =&gt; 60,&nbsp;&nbsp;& nbsp ' X ' =&gt; 61,&nbsp;&nbsp;&nbsp; ' Y ' =&gt; 62,&nbsp;&nbsp;&nbsp; ' Z ' =&gt; 63,&nbsp;);


$result = 0;


$len = strlen ($sixty _four);





for ($n = 0; $n &lt; $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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.