Home  >  Article  >  Backend Development  >  How to implement automatic file renaming in PHP, _PHP tutorial

How to implement automatic file renaming in PHP, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:13687browse

How to implement automatic file renaming in PHP,

The example in this article describes the implementation method of automatically renaming files in PHP. Share it with everyone for your reference. The specific method analysis is as follows:

PHP rename file names are often used in the actual development process. For example, when users upload files or some cache files are automatically generated, we need to use the automatic rename function. But generally when we create uploaded files, we use the method of naming the current system time plus a random number. Although this method is feasible, it sometimes does not meet the needs of customers. Some customers require that our file naming method should be automatically serialized like the Windows system. For example, when someone uploads a file named "New Text Document" and someone else uploads a file named "New Text Document", we will use the serial number. The meaning of the name is that the second "New Text Document" will be named "New Text Document (1)" when someone uploads a file with the same name and so on.
Let me share a source code with you:

Copy code The code is as follows:
$file = dirname(__FILE__).'/New text document.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;
                                                                                                        break;
                                                                                                                                     }                                                                                                    If (fopen($file, 'w')) {$Msg = 'Created successfully'.$file;}
Return $Msg;
}
?>


I hope this article will be helpful to everyone’s PHP programming design.

http://www.bkjia.com/PHPjc/906110.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/906110.htmlTechArticleHow to implement automatic renaming of files in PHP. This article describes the implementation of automatic renaming of files in PHP. Share it with everyone for your reference. The specific method is analyzed as follows: PHP renames the file name...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn