Content · Programming · PHP

PHP copy() Function: Usage and Examples

Learn how PHP copy works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Copies a file from one path to another.

What Is copy() in PHP?

Copies a file from one path to another.

Its syntax is:

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

Parameters:

  • $from — Accepted as string; required argument.
  • $to — Accepted as string; required argument.
  • $context — Accepted as ?resource; optional default null.

Return type: bool. copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.

Availability: Filesystem access required.

Example 1: Basic Usage

<?php

$sourceFile = tempnam(sys_get_temp_dir(), "src_"); $targetFile = $sourceFile . ".copy"; file_put_contents($sourceFile,"hello"); var_dump(copy($sourceFile,$targetFile)); unlink($sourceFile); unlink($targetFile);

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.

Example 2: A Different Input

<?php

$sourceFile = tempnam(sys_get_temp_dir(), "src_"); $targetFile = $sourceFile . ".copy"; file_put_contents($sourceFile,"data"); copy($sourceFile,$targetFile); echo file_get_contents($targetFile); unlink($sourceFile); unlink($targetFile);

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.

Practical Example: Applying It in a Program

<?php

$sourceFile = tempnam(sys_get_temp_dir(), "src_"); $backupFile = $sourceFile . ".bak"; file_put_contents($sourceFile,"report"); if (copy($sourceFile,$backupFile)) { echo "Backup created"; } unlink($sourceFile); if (is_file($backupFile)) { unlink($backupFile); }

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.

Important Behavior and Edge Cases

  • Key behavior: copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.
  • Environment: Filesystem access required.

Comparison With Related Functions

FunctionPurpose
copy()Copies a file from one path to another.
rename()rename() moves or renames a file; copy() preserves the original while creating a destination.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: copy() can overwrite a destination file and does not validate file contentscopy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.

Key Takeaways

  • Copies a file from one path to another.
  • copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.
  • Filesystem access required.

Summary: copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.

Share this resourceSend this page to someone who may find it useful.