FrenamePHP flexible file naming function frename_PHP tutorial

Source: Internet
Author: User
FrenamePHP flexible file naming function frename. To solve this problem, some methods are to rename the new file with the timestamp, and some are to increment the serial number to solve the problem, I recently compiled a frename letter to deal with this situation. some methods are to rename the new file with the timestamp, and some are to use the serial number incrementing method, in order to solve the trouble, I have recently compiled a frename function to flexibly obtain the new names of uploaded files based on custom rules.

The code is as follows:


Function frename ($ file, $ rule = '{timestamp}', $ force = true ){
/*-----------------------
* Author: m35
* Date: 2009/8/11
* Obtain a new name for a file based on the naming rules to process the existing file name when uploading files.
* @ Parm1 $ file -- physical file path
* @ Parm2 $ rule -- naming rule. the default value is the timestamp.
* @ Parm3 $ force -- whether to forcibly name the object. If yes, the object will be named even if the object does not exist. the default value is mandatory.
* Retrun str -- new file path named by $ rule
* Example:
Echo 'default rule (timestamp): '. frename (_ FILE __);
Echo'
Sequence Number incrementing rule: '. frename (_ FILE __,' {name} {n }');
Echo'
Rules for increasing the numbers of n leading 0: '. frename (_ FILE __,' {name} {5n }');
Echo'
Sequence Number increment rules with delimiters _ and three leading 0: '. frename (_ FILE __,' {3n} <_> {name }');
Echo'
Rule for combination of time and date elements :'. frename (_ FILE __, '{y }_{ m }_{ d }_{ h }_{ I }_{ s }');
Echo'
Rule for combination of time and date elements in the abbreviated form :'. frename (_ FILE __, '{Y }_{ M }_{ D }_{ H }_{ I }_{ s }');
Echo'
Other custom combinations: '. frename (_ FILE __,' {timestamp }_{ name} <_> {n }');
-----------------------*/
If (! $ Force &&! File_exists ($ file) return $ file;
$ Filename = basename ($ file );
$ Path = str_replace ($ filename, '', $ file );
$ Suffix = substr ($ filename, strrpos ($ filename ,'.'));
$ Name = str_replace ($ suffix, '', $ filename );
$ Timestamp = time ();
List ($ y, $ Y, $ m, $ M, $ d, $ D, $ h, $ H, $ I, $ s) = explode (',', date ('y, Y, m, n, d, j, h, G, I, s '));
$ Tempname = str_replace (
Array ('{name}', '{timestamp}', '{y}', '{Y}', '{m}', '{M }', '{d}', '{D}', '{h}', '{H}', '{I}', '{s }'),
Array ($ name, $ timestamp, $ y, $ Y, $ m, $ M, $ d, $ D, $ h, $ H, $ I, $ s ),
$ Rule
);
If (preg_match ('/\ {(\ d ?) N \}/', $ rule, $ n )){
Preg_match ('/<([^>] +)>/', $ tempname, $ sep );
$ File = $ path. str_replace (array ($ n [0], $ sep [0]), array ('',''), $ tempname). $ suffix;
If (! File_exists ($ file) return $ file;
$ Tempname = str_replace ($ sep [0], $ sep [1], $ tempname );
$ Tname = $ tempname;
$ I = 1;
Do {
$ Nn = sprintf ("% 0 {$ n [1]} s", $ I );
$ Tempname = str_replace ($ n [0], $ nn, $ tname );
$ File = $ path. $ tempname. $ suffix;
}
While (file_exists ($ file ));
Return $ file;
} Else {
$ File = $ path. $ tempname. $ suffix;
If (file_exists ($ file) return false;
Else return $ path. $ tempname. $ suffix;
}
}
Echo 'default rule (timestamp): '. frename (_ FILE __);
Echo'
Sequence Number incrementing rule: '. frename (_ FILE __,' {name} {n }');
Echo'
Rules for increasing the numbers of n leading 0: '. frename (_ FILE __,' {name} {5n }');
Echo'
Sequence Number increment rules with delimiters _ and three leading 0: '. frename (_ FILE __,' {3n} <_> {name }');
Echo'
Rule for combination of time and date elements :'. frename (_ FILE __, '{y }_{ m }_{ d }_{ h }_{ I }_{ s }');
Echo'
Rule for combination of time and date elements in the abbreviated form :'. frename (_ FILE __, '{Y }_{ M }_{ D }_{ H }_{ I }_{ s }');
Echo'
Other custom combinations: '. frename (_ FILE __,' {timestamp }_{ name} <_> {n }');
?>

...

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.