Frename PHP flexible File naming function frename

Source: Internet
Author: User

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

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.