Home PHP Filesystem copy() PHP copy() Function Definition In PHP, copy() makes a copy of a file. The original file stays where it is, and a new file with the same content is created at the new path.
If the destination file already exists, copy() overwrites it without asking. The function copies single files only, so to copy a whole folder you need to loop over its files.
Syntax Call copy() with the values you need. Start with this line.
$ok = copy(__DIR__ . "/demo.txt", __DIR__ . "/demo-copy.txt"); Call What it does copy("a.txt", "b.txt")Copies a.txt to a new file called b.txt. copy($file, "backup/" . basename($file))Copies a file into another folder with the same name. copy("https://example.com/logo.png", "logo.png")Downloads a remote file when allow_url_fopen is on.
The destination folder must already exist. If the destination is a folder instead of a file name, copy() fails, so build the full target path first.
Library declarations copy(string $from, string $to, ?resource $context = null): bool Parameters Parameter Description from Required. The path of the file you want to copy. to Required. The path of the new file. An existing file is overwritten. context Optional. A stream context created with stream_context_create(). Rarely needed.
Return Value Return Value Description bool true on success, false on failure.
PHP Examples 1. Copy a File 2. Copy Overwrites an Existing File 3. Copy a File into a Backup Folder Example 1 — Copy a File Create a small file, copy it, and show that both files have the same content.
<?php
$source = __DIR__ . "/demo.txt";
$target = __DIR__ . "/demo-copy.txt";
file_put_contents($source, "Hello CodeToFun");
if (copy($source, $target)) {
echo file_get_contents($target), PHP_EOL;
}
unlink($source);
unlink($target); How It Works file_put_contents() creates the source file with some text. copy() makes a new file at the target path. file_get_contents() reads the copy and we print its text. unlink() removes both demo files at the end. Example 2 — Copy Overwrites an Existing File If the target already exists, copy() replaces its content. Check first when that is not what you want.
<?php
$source = __DIR__ . "/new.txt";
$target = __DIR__ . "/old.txt";
file_put_contents($source, "new content");
file_put_contents($target, "old content");
if (file_exists($target)) {
echo "Target exists, it will be replaced.", PHP_EOL;
}
copy($source, $target);
echo file_get_contents($target), PHP_EOL;
unlink($source);
unlink($target); Target exists, it will be replaced.
new content How It Works Both files are created with different text. file_exists() tells us the target is already there. copy() writes the source content over the target file. Reading the target now shows new content instead of the old text. Frequently Asked Questions What does copy() do in PHP? It copies a file from one path to another. The original file is not changed or removed.
Does copy() overwrite an existing file? Yes. If the destination already exists, its content is replaced. Use file_exists() first if you want to avoid that.
Can copy() copy a whole folder? No. It handles one file at a time. To copy a folder, loop over its files with scandir() or glob() and call copy() for each one.
Why does copy() fail? Usual reasons are a missing source file, a destination folder that does not exist, or no write permission on the destination.
What is the difference between copy() and rename()? copy() keeps the original file and creates a second one. rename() moves the file, so the original path no longer exists.
Next: delete there is no real function called delete(). The manual page for delete only points you to u.
delete tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion