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 }');
?>
...