copy() example


 

copy() example

Here you will learn the example of copy() in PHP.

Here you will learn the example of copy() in PHP.

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".

Ads