What is the best way to copy a file and rename the copy using PHP?
To copy a file and rename the copy using PHP, you can use the `copy()` function to make a duplicate of the file and then use the `rename()` function to rename the copied file to the desired name.
$fileToCopy = 'originalFile.txt';
$newFileName = 'copiedFile.txt';
if (copy($fileToCopy, $newFileName)) {
echo "File copied successfully.";
if (rename($newFileName, 'newName.txt')) {
echo "File renamed successfully.";
} else {
echo "Error renaming file.";
}
} else {
echo "Error copying file.";
}
Keywords
Related Questions
- How important is it for PHP developers to regularly update and patch their forums to maintain security?
- What are some best practices for creating a PHP program to play Battleship?
- What are some alternative methods to the file() function for reading a webpage into an array in PHP to avoid errors like "failed to open stream"?