PHP Function Reference

PHP copy() Function



The PHP copy() function makes a copy of the file source to dest.

Syntax

copy(source, dest, context)

Parameters

source Required. Specify the path to the source file.
dest Required. Specify the destination path. If it is a URL, the copy operation may fail if the wrapper does not support overwriting of existing files.
If the destination file already exists, it will be overwritten.
context Optional. Specify the context resource created with stream_context_create() function.

Return Value

Returns true on success or false on failure.

Example:

Lets assume that we have a file called test.txt in the current working directory. The example below demonstrates on using this function to make a copy of this file.

<?php
$source = 'test.txt';
$dest = 'demo.txt';

//making a copy of $file
if(copy($source, $dest)){
  echo "$source is successfully copied as $dest.\n";
} else {
  echo "An error encountered while copying $source.\n";
}
?>

The output of the above code will be:

test.txt is successfully copied as demo.txt.

❮ PHP Filesystem Reference