What Is File Copying in PHP? See Examples

Copying files is a very common task in PHP, especially when you need to create backups, duplicate uploaded files, move templates, or generate multiple versions of the same file. PHP provides a simple and powerful function to handle file copying: copy().

This guide covers how copying works, error handling, permissions, best practices, and real-world use cases.


What Is File Copying in PHP?

Copying a file means creating a duplicate of an existing file at a new location with the same or different name.

PHP makes this very easy with:

copy("source.txt", "destination.txt");

Basic Syntax of copy()

copy(source, destination);
  • source → the existing file
  • destination → new file path or file name

The function returns:

  • true → if file copied successfully
  • false → if copying fails

Simple Example: Copy a File

<?php
if (copy("data.txt", "backup_data.txt")) {
    echo "File copied successfully!";
} else {
    echo "Failed to copy file.";
}
?>

This creates a duplicate named backup_data.txt.


Copying a File to Another Directory

copy("images/photo.jpg", "backup/photo_backup.jpg");

Make sure the destination folder exists and has write permissions.


Copy and Rename at the Same Time

copy("resume.pdf", "resume_old_version.pdf");

Useful for version control.


Copying Files Uploaded by Users

copy($_FILES['file']['tmp_name'], "uploads/" . $_FILES['file']['name']);

Used in:

  • Upload systems
  • Document management
  • Media libraries

Checking Before Copying

Always check if the source file exists:

if (file_exists("info.txt")) {
    copy("info.txt", "info_copy.txt");
} else {
    echo "Source file not found.";
}

Handling Errors Properly

if (!copy("report.txt", "backup/report_backup.txt")) {
    echo "Error: Could not copy report.txt!";
} else {
    echo "Backup created!";
}

Copying With Permission Checking

Some systems require permissions:

if (!is_writable("backup")) {
    echo "Backup folder is not writable.";
} else {
    copy("file.txt", "backup/file.txt");
}

Real-World Use Cases of Copying Files in PHP

Creating automatic backups
Duplicating templates
Versioning documents
Storing uploaded files safely
Creating image thumbnails (copy then resize)
Copying system logs before clearing them
Copying configuration files


Copying Multiple Files (Loop Example)

$files = ["a.txt", "b.txt", "c.txt"];

foreach ($files as $file) {
    copy($file, "backup/" . $file);
}

Best Practices

✔ Check if the source file exists
✔ Check destination folder permissions
✔ Never copy extremely large files without size checks
✔ Use meaningful filenames for backups
✔ Avoid overwriting without warning
✔ Use is_writable() and file_exists() to avoid errors


Alternatives to copy()

PHP offers other file movement functions:

FunctionPurpose
rename()Moves or renames a file
file_put_contents()Replace/create files
stream_copy_to_stream()Copy file streams

Citations

🔗 View other articles about PHP:
http://savanka.com/category/learn/php/

🔗 External PHP Documentation:
https://www.php.net/manual/en/function.copy.php

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *