copy()

copy() — Copies file.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

If you wish to move a file, use the rename() function.

Signature

copy(string $from, string $to, ?resource $context = null): bool

Parameters

ParameterTypeWhat it takes
$fromstringPath to the source file.
$tostringThe destination path. If to is a URL, the copy operation may fail if the wrapper does not support overwriting of existing files.
$context?resourceA valid context resource created with stream_context_create().

Return value

Returns true on success or false on failure.

Example

A runnable snippet:

$file = 'example.txt'; $newfile = 'example.txt.bak'; if (!copy($file, $newfile)) { echo "failed to copy $file...\n"; }

See also

move_uploaded_file(), rename()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page copy() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.