Home Tutorial Php Examples copy() example

 
 

copy() example
Posted on: July 30, 2009 at 12:00 AM
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".

Related Tags for copy() example:


Ask Questions?

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.