Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

copy

Copies a file.

Syntax

copy(string $from, string $to): bool

Parameters

ParameterTypeDescription
$fromstringThe path to the source file.
$tostringThe path of the copy.

Return value

bool`true` if the file was copied.

Examples

A backup copy of a file:

<?php
 
$file = sys_get_temp_dir() . '/ref-copy.txt';
file_put_contents($file, 'data');
 
copy($file, $file . '.bak');
 
echo file_get_contents($file . '.bak');

Output:

data

Notes

If the destination file already exists, it is overwritten without warning.

See also

Related recipes

In the tutorial

  • Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.

Thanks for reading!

Donate to the site — we work hard for you.