To solve this problem, some methods are to rename the new file with a timestamp, and some are to increment the serial number. To solve the problem, I recently compiled a frename function, you can use custom rules to obtain the new names of uploaded files.
Copy codeThe Code is as follows:
<? Php
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 '<br/> sequence number incrementing rule:'. frename (_ FILE __, '{name} {n }');
Echo '<br/> rules for increasing the sequence numbers of n leading 0:'. frename (_ FILE __,' {name} {5n }');
Echo '<br/> use the separator <span style = "color: # E00;" >_</span> and follow the sequence increasing rule with three leading zeros :'. frename (_ FILE __, '{3n} <_> {name }');
Echo '<br/> rule for combination of time and date elements :'. frename (_ FILE __, '{y }_{ m }_{ d }_{ h }_{ I }_{ s }');
Echo '<br/> rule for combination of time and date elements in abbreviated form :'. frename (_ FILE __, '{Y }_{ M }_{ D }_{ H }_{ I }_{ s }');
Echo '<br/> 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 '<br/> sequence number incrementing rule:'. frename (_ FILE __, '{name} {n }');
Echo '<br/> rules for increasing the sequence numbers of n leading 0:'. frename (_ FILE __,' {name} {5n }');
Echo '<br/> use the separator <span style = "color: # E00;" >_</span> and follow the sequence increasing rule with three leading zeros :'. frename (_ FILE __, '{3n} <_> {name }');
Echo '<br/> rule for combination of time and date elements :'. frename (_ FILE __, '{y }_{ m }_{ d }_{ h }_{ I }_{ s }');
Echo '<br/> rule for combination of time and date elements in abbreviated form :'. frename (_ FILE __, '{Y }_{ M }_{ D }_{ H }_{ I }_{ s }');
Echo '<br/> other custom combinations:'. frename (_ FILE __,' {timestamp }_{ name} <_> {n }');
?>