Implementation method of PHP automatic renaming file

  • 2021-07-26 07:00:20
  • OfStack

In this paper, an example is given to describe the implementation method of PHP automatic renaming file. Share it for your reference. Specific methods are analyzed as follows:

PHP renaming file names we often use in the actual development process, such as users upload files or some cache files automatically generated functions we all need to use the automatic renaming function. But 1 we are in the production of upload file naming methods are used to take the system's current time plus at any time in the way, this method is feasible but sometimes can not meet the needs of customers. Some customers require our file name naming method to be automatic pipeline numbering like windows system 1. For example, upload a file named "New Text Document". When another person uploads a file named "New Text Document", we use serial number to name it. It means automatically, but the second "New Text Document" is named "New Text Document (1)". When another person uploads a file with the same name, and so on.

Here is a source code to share with you:


<?php  
$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;  
              }else{  
                     break;  
              }  
       }  
       if (fopen($file, 'w')) {$Msg = ' Successful creation  '.$file;}  
       return $Msg;  
}  
?>

Of course, in the actual process, 1 is generally named according to the current date

php rename () Function

The rename () function can rename a file or directory, returning TRUE on success and FALSE on failure. The syntax format of this function is as follows:
rename(string $oldname, string $newname[, resource $context])

Where $oldname is the file name to modify; $newname is the new file name; $context is an optional parameter that specifies the environment for the file handle. $context is a set of options that can modify the flow's behavior.

"Sample" uses the rename () function to rename a file.


<?php
    $file = 'test.txt';
    if(file_exists($file)){
        if(rename($file,'newtest.txt')){
            echo $file.'  Rename successful! ';
        }else{
            echo $file.'  Rename failed! ';
        }
    }else{
        echo $file.'  It doesn't exist! ';
    }
?>

Running the above code renames the root test. txt file to newtest. txt and outputs 1:

test. txt renamed successfully!

There are several points to note when using the rename () function:

For non-empty folders, they can only be moved under the same 1 drive letter;
For empty folders, rename () can be moved between different drive letters. However, the parent directory of the destination folder must exist;
For files, rename () can also move between different drive letters.

I hope this article is helpful to everyone's PHP programming.


Related articles: