PHP copy() Function
Definition
The copy() function copies a file.
Syntax
PHP copy() Function has the following syntax.
copy(file,to_file)
Parameter
Parameter | Is Required | Description |
---|---|---|
file | Required. | File to copy |
to_file | Required. | File to copy to |
Note
If the destination file already exists, it will be overwritten.
Return
This function returns TRUE on success and FALSE on failure.
Example 1
Files are copied using copy().
<?php
echo copy("source.txt","target.txt");
?>
Example 2
copy() takes two parameters: the filename you wish to copy from and the filename you wish to copy to.
<?PHP// w w w . j a va2 s . c o m
$filename = "c:/abc/test.txt";
$filename2 = $filename . '.old';
$result = copy($filename, $filename2);
if ($result) {
print "$filename has been copied to $filename2.\n";
} else {
print "Error: couldn't copy $filename to $filename2!\n";
}
?>