copy
Copies a file.
Syntax
copy(string $from, string $to): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $from | string | The path to the source file. |
| $to | string | The 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:
dataNotes
If the destination file already exists, it is overwritten without warning.
See also
- rename — Renames or moves a file.
- file_put_contents — Writes data to a file.
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.