PHP image upload and rename methods

Source: Internet
Author: User
PHP image upload and rename methods

  1. /*
  2. Com_create_guid () is a function supported by php5. for unsupported versions, you can define them by yourself.
  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, 12, 4). $ hyphen
  16. . Substr ($ charid, 16, 4). $ hyphen
  17. . Substr ($ charid, 20, 12)
  18. . Chr (1, 125 );//"}"
  19. Return $ uuid;
  20. }
  21. }

2. similar to the guid, MD5 outputs a 32-character hexadecimal number. The difference is that the guid is generated randomly. md5 is generated based on the input data. Example:

  1. $ Str = "Hello ";
  2. Echo md5 ($ str );
  3. ?>

Output: 8b1a9953c4611296a827abf8c47804d7 advantages: the output value can be controlled based on the input seed data. if the seed data is regular and does not overlap, md5 can be used to protect the data, this produces great obfuscation. Disadvantages: 32-bit characters are too long; non-repeated seed data must be provided; usage: high concurrency, in seconds as the seed data, there will still be duplication.

  1. /*
  2. * Used in combination with the time () function, the number of seconds from January 1, 1970 to the current time is used as the number of seeds.
  3. */
  4. $ Str = time ();
  5. Echo md5 ($ str );
  6. ?>

3. uniqid (): returns a string of 13 or 23 characters. for our purpose, uniqid () is like an md5 () character version. in particular, we can use differential identification as the string prefix, this reduces the chances of duplicate names. In extreme cases such as non-high concurrency, we recommend that you use this function to meet general requirements. Description: define: the uniqid () function generates a unique ID based on the current time in microseconds. usage: uniqid (prefix, more_entropy); description: prefix can be used to add a prefix to the output string. for example, if the more_entropy parameter is true, a 23-bit string is output.

  1. Var_dump (uniqid ());
  2. Var_dump (uniqid (""));
  3. ?>

Output result: string (13) "51734aa562254" string (14) "a51734aa562257" advantage: 13-bit string length, which is an acceptable file name length. you can add a prefix, the results contain data obfuscation, which can avoid reverse pushing of the original data. Disadvantages: Similar to md5, high concurrency, and seed data in seconds, duplication still occurs. III. upgrade solution 1. fast_uuid: return 17 digits, which is a bit like an incomplete customized version of uniqid (). The concept of "start time of seeds" in this function is very enlightening. The default time used in time () and uniqid () is calculated from January 1, 1970, with a length of 10 (1366512439). This value can be reduced using the "seed start time, because what we actually need is only a value that can automatically increase. After the start time is customized, in addition to reducing the length, it can also be confused.

  1. /*
  2. * The suffix_len parameter specifies the number of random numbers appended 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. //! Start time for calculating the number of seeds
  8. $ Being_timestamp = strtotime ('2017-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. }

Output: 29832412631099013 2. time () + random number. in the preceding example, a random number is used to solve multiple requests in one second. Two functions are provided:

  1. Function random ($ length ){
  2. $ Hash = '';
  3. $ Chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxy ';
  4. $ Max = strlen ($ chars)-1;
  5. PHP_VERSION <'4. 100' & mt_srand (double) microtime () * 2.0 );
  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 (), 16, $ numeric? 10: 35 );
  14. $ Seed = $ numeric? (Str_replace ('0', '', $ seed). '000000'): ($ 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. ?>

IV. Final Solution: userid + second + random number. The "userid + second" 10-to 64-to-64, reducing the number of digits. Note: 1. userid: 64-digit maximum value "ZZZZ" is converted to decimal equal to "16777215", and "ZZZ" is converted to decimal maximum value equal to "262143"; 2. seconds: set your own time start point. $ Less = time ()-strtotime ('1970-4-21') to 64-digit "1SpRe", 5-bit $ less = time () -strtotime ('1970-3-21 ') is converted to 64-digit "_ jHY", 4-digit 3. random number: use random (3) to generate a three-digit random number. Final result: 4-digit userid + 4-digit second + 3-digit random number = 11-digit string. Although the results are similar to those of uniqid (), the robustness is improved.12. Next page

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.