Copying a file in PHP
In this example, you will learn 'how to copy a file in PHP programming language?'
For copying a file, we use copy command from source to destination within PHP code tag like
copy ($source,$destination);
Lets do it practically.
First begin the PHP tag (<?php), define the source path and destination under $source and $destination respectively.
Apply the conditional statement to print the output result.
<?php
$source = "C:\\wamp\\www\\projects\\public_html\\upload\\mytextfile.txt";
$destination = "C:\\wamp\\www\\projects\\public_html\\upload\\myfile.txt";
if(copy($source, $destination)) {
echo "File copied successfully.", "\n";
} else {
echo "The file not copied. Please try again.", "\n";
}
?>
In the above example, if file be exists and copied successfully, the print will be "File copied successfully." otherwise the output will be "The file not copied. Please try again".
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.