PHP copy() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

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.

PHP
$ok = copy(__DIR__ . "/demo.txt", __DIR__ . "/demo-copy.txt");
CallWhat 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
PHP
copy(string $from, string $to, ?resource $context = null): bool

Parameters

ParameterDescription
fromRequired. The path of the file you want to copy.
toRequired. The path of the new file. An existing file is overwritten.
contextOptional. A stream context created with stream_context_create(). Rarely needed.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Copy a File

Create a small file, copy it, and show that both files have the same content.

PHP
<?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

  1. file_put_contents() creates the source file with some text.
  2. copy() makes a new file at the target path.
  3. file_get_contents() reads the copy and we print its text.
  4. 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
<?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);

How It Works

  1. Both files are created with different text.
  2. file_exists() tells us the target is already there.
  3. copy() writes the source content over the target file.
  4. Reading the target now shows new content instead of the old text.

Example 3 — Copy a File into a Backup Folder

Create a folder if needed and copy the file into it with the same name.

PHP
<?php
$source = __DIR__ . "/report.txt";
$folder = __DIR__ . "/backup";

file_put_contents($source, "Monthly report");

if (!is_dir($folder)) {
    mkdir($folder);
}

$target = $folder . "/" . basename($source);

if (copy($source, $target)) {
    echo "Copied to backup/", basename($target), PHP_EOL;
}

unlink($source);
unlink($target);
rmdir($folder);

How It Works

  1. We create a report file and decide on a backup folder.
  2. is_dir() and mkdir() make sure the folder exists.
  3. basename() reuses the original file name for the target path.
  4. copy() does the work, and the cleanup lines remove the demo files and folder.

Frequently Asked Questions

It copies a file from one path to another. The original file is not changed or removed.
Yes. If the destination already exists, its content is replaced. Use file_exists() first if you want to avoid that.
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.
Usual reasons are a missing source file, a destination folder that does not exist, or no write permission on the destination.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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