PHP image upload and rename six ways to summarize

Source: Internet
Author: User
Tags sprintf unique id uuid
    1. /*
    2. Com_create_guid () is a feature supported by the PHP5 version and can be defined on its own for unsupported versions
    3. */
    4. function guid () {
    5. if (function_exists (' Com_create_guid ')) {
    6. return Com_create_guid ();
    7. }else{
    8. Mt_srand (Double) microtime () *10000)//optional for PHP 4.2.0 and up.
    9. Echo (Mt_rand ());
    10. $charid = Strtoupper (MD5 (Uniqid (rand (), true));
    11. $hyphen = Chr (45);//"-"
    12. $uuid = Chr (123)//"{"
    13. . substr ($charid, 0, 8). $hyphen
    14. . substr ($charid, 8, 4). $hyphen
    15. . substr ($charid, 4). $hyphen
    16. . substr ($charid, 4). $hyphen
    17. . substr ($charid, 20,12)
    18. . chr (125);//"}"
    19. return $uuid;
    20. }
    21. }
Copy Code

2, MD5 and GUID will output 32 character hexadecimal number, the difference is that the GUID is randomly generated, MD5 need to be generated according to the input data. Example:

    1. $str = "Hello";
    2. echo MD5 ($STR);
    3. ?>
Copy Code

Output: 8b1a9953c4611296a827abf8c47804d7 Advantages: Can be based on the input of the seed data to control the output value, if the seed data is not repeated, through the MD5 can protect the data, resulting in a great confusion; disadvantage: 32-bit characters are too long ; the need to provide non-duplicated seed data; usage: High concurrency, with seconds as the seed data, there will still be repetition.

    1. /*
    2. * Combined with the time () function, the number of seconds from 1970 years to the current period as the seed count
    3. */
    4. $str =time ();
    5. echo MD5 ($STR);
    6. ?>
Copy Code

3, Uniqid (): Returns a 13 or 23-bit string for our purposes, uniqid () is like an improved version of MD5 (), especially if we can use the differential identifier as a string prefix to reduce the chance of duplicate naming occurrences. For extreme situations such as non-high concurrency, it is recommended to use this function to meet general requirements. 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, as shown below, more_ When the entropy parameter is true, a 23-bit string is output.

    1. Var_dump (Uniqid ());
    2. Var_dump (Uniqid ("a"));
    3. ?>
Copy Code

The output is: string ("51734aa562254" string "a51734aa562257" advantage: 13-bit string length, is an acceptable file name length, you can add a prefix, the result contains data confusion, to avoid the reverse of the original data ; disadvantage: Similar to MD5, high concurrency, in seconds as the seed data, there will still be repeated phenomenon. Third, the upgrade version of Scenario 1, Fast_uuid: Return 17 digits somewhat like uniqid () of the incomplete custom version, the function of the "seed number start time" concept is very enlightening. Time () and uniqid () are used by default in 1970, with a length of 10 bits (1366512439) and a "seed count start time" to narrow this value, because what we actually need is just a value that can be automatically increased. Since the start time customization, in addition to reducing the length, but also can play a role of confusion.

    1. /*
    2. * Parameter Suffix_len Specifies how many bits of random number are attached to the generated ID value, the default value is 3
    3. * @param int Suffix_len
    4. * @return String
    5. */
    6. function Fast_uuid ($suffix _len=3) {
    7. //! Calculate the start time of the seed number
    8. $being _timestamp = strtotime (' 2013-3-21 ');
    9. $time = Explode (' ', Microtime ());
    10. $id = ($time [1]-$being _timestamp). sprintf ('%06u ', substr ($time [0], 2, 6));
    11. if ($suffix _len > 0)
    12. {
    13. $id. = substr (sprintf ('%010u ', Mt_rand ()), 0, $suffix _len);
    14. }
    15. return $id;
    16. }
Copy Code

Output: 29832412631099013 2, time () + random number in the above example, the use of random numbers has occurred in order to resolve multiple requests occurring in one second. provides two functions:

  1. function Random ($length) {
  2. $hash = ";
  3. $chars = ' abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ';
  4. $max = strlen ($chars)-1;
  5. Php_version < ' 4.2.0 ' && Mt_srand ((double) microtime () * 1000000);
  6. for ($i = 0; $i < $length; $i + +) {
  7. $hash. = $chars [Mt_rand (0, $max)];
  8. }
  9. return $hash;
  10. }
  11. function Random2 ($length, $numeric = 0) {
  12. Php_version < ' 4.2.0 '? Mt_srand (Double) microtime () * 1000000): Mt_srand ();
  13. $seed = Base_convert (MD5 (Print_r ($_server, 1). Microtime ()), $numeric? 10:35);
  14. $seed = $numeric? (Str_replace (' 0 ', ', $seed). ' 012340567890 '): ($seed. ' ZZ '. Strtoupper ($seed));
  15. $hash = ";
  16. $max = strlen ($seed)-1;
  17. for ($i = 0; $i < $length; $i + +) {
  18. $hash. = $seed [Mt_rand (0, $max)];
  19. }
  20. return $hash;
  21. }
  22. ?>
Copy Code

Four, the final plan idea: userid+ second + random number. where "userid+ seconds" 10 binary to 64, reducing the number of bits. Description: 1, userid:64 binary maximum "ZZZZ" is converted to decimal equals "16777215", "ZZZ" is converted to decimal maximum equals "262143", 2, second: Set its own time starting point. $less =time ()-strtotime (' 2012-4-21′) converted to 64 binary "1SpRe", 5-bit $less=time ()-strtotime (' 2013-3-21′) converted to 64 "_jhy", 4-bit 3, Random number: Generates 3-bit random numbers using random (3). Final Result: 4-bit userid+4 bit seconds + 3-bit random number = 11-bit string. Although the results look similar to the uniqid (), the strength has improved. 1 2 Next last page

  • Related Article

    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.