This article describes how to automatically rename a file in PHP. it is a very practical technique to implement automatic sequential number naming like Windows, for more information about how to automatically rename a PHP file, see the example in this article. Share it with you for your reference. The specific method is analyzed as follows:
PHP rename file names are often used in actual development. for example, you need to use the automatic rename function to upload files or automatically generate cached files. However, when creating and uploading files, we usually use the method of getting the current time of the system plus the number of files at any time. This method is feasible, but sometimes it cannot meet the needs of customers. Some customers require that our file names be named in the same way as the windows system, for example, upload a file named "New Text Document". when another person uploads a file named "New Text Document", we use the serial number to name it, which means that the second "new text document" is automatically created" name it "new text document (1) "When someone uploads a file with the same name, and so on.
The following is a source code:
The code is as follows:
<? Php
$ File = dirname (_ FILE _). '/create a text file. txt ';
Echo L_rename ($ file );
Function L_rename ($ file ){
$ ICount = 0;
$ File_type = strrchr ($ file ,'.');
$ FilePath = substr ($ file, 0, strrpos ($ file ,'.'));
While (true ){
If (is_file ($ file )){
+ + $ ICount;
$ File = $ FilePath. '('. $ iCount. ')'. $ File_type;
} Else {
Break;
}
}
If (fopen ($ file, 'w') {$ Msg = 'created successfully '. $ file ;}
Return $ Msg;
}
?>
I hope this article will help you with PHP programming.