rename a file in php

To rename a file in PHP, you can use the rename function. The rename function takes two arguments: the current file name (including path) and the new file name (including path). Here's an example:

main.php
// Current file name
$old_name = 'path/to/current_file.txt';

// New file name
$new_name = 'path/to/new_file.txt';

// Rename the file
if (rename($old_name, $new_name)) {
    echo "File renamed successfully.";
} else {
    echo "File renaming failed.";
}
255 chars
13 lines

In the above example, we first define the current file name and the new file name. We then use the rename function to rename the file. This function returns a boolean value (true if the file was renamed successfully and false otherwise), so we use an if/else statement to check if the renaming was successful and display an appropriate message.

Make sure that you have the appropriate permissions to rename the file.

gistlibby LogSnag