In PHP, you can use rename() Use a function to rename a file.
Here is an example of uploading a file. oldfile.txt Rename newfile.txt:
<?php
$old_filename = 'oldfile.txt';
$new_filename = 'newfile.txt';
if (rename($old_filename, $new_filename)) {
echo "文件重命名成功.";
} else {
echo "文件重命名失败.";
}
?>In the above code,rename() The function accepts two parameters: the original file name and the new file name. If the renaming is successful, the function returns. trueOtherwise, return. false。
Please note that,rename() This function can only be used to rename files; it cannot move files to another directory. If you need to move a file to another directory, you can use move_uploaded_file() Function or copy() function.