https://dev.webpages.dk/  
 
PHP How to copy and rename a File
How to copy and rename a File in one go:

This small snippet will show you how you can copy a file, and rename it, in the same go.

We will use the command 'copy (sourcefile, destinationfile)' for this, and we even see how you can react on if there is a problem happening while copying.

 Copy a File, and rename it

How do you Copy and Rename a file using PHP?

When you will like to copy a file, and maybe rename it at the same time, then you can use this very simple piece of code.

 PHP

 When you will like to copy a file, and maybe rename it at the same time, then you can use this very simple piece of code.

<?php
    $sourcefile = 'file.txt';
    $destinationfile = './backup/file.bak';
    copy($sourcefile, $destinationfile)
?>
.. but I really like this next example, doing the error checking in same line as the Copy function, simply. It happens by checking 'if' there is an error with the copy. ('!' - meaning NOT), so if NOT copying (caused by an error), then send an error message to the screen.
<?php
    $sourcefile = 'file.txt';
    $destinationfile = './backup/file.bak';
    if (!copy($sourcefile, $destinationfile)) {
    echo "Error trying to copy $sourcefile!";
}
?>
Icons made by Freepik from www.flaticon.com Here we will see how to copy a file and rename it in the same go.
01:08:11