How to Copy File in PHP

In this tutorial, we will learn how to copy a file in PHP. The easiest way to copy a file in PHP is by using the copy() function. The copy() function is a built-in PHP function used to copy files. This function allows you to make a duplicate of an existing file and place it in the same or another directory.

Syntax of copy() Function in PHP


The basic syntax to define copy() function in PHP is as:

bool copy(string $source, string $destination, ?resource $context = null)

Parameters:

The copy() function accepts three parameters:

  • $source → This parameter specifies the path of the file you want to copy.
  • $destination → This parameter specifies the new file name or path of the copied file. In other words, it specifies the location where the copy should be created.
  • $context (optional) → A stream context parameter, usually not required for basic usage.

Return Value:

  • If the operation succeeds, the copy() function returns true; otherwise, it returns false.

Simple Example of Copying a File in PHP


Example 1: Copying a File

<?php
$source = "originalfile.txt";
$destination = "copyfile.txt";

if (copy($source, $destination)) {
     echo "File copied successfully!";
} else {
     echo "Failed to copy file.";
}
?>
Output:
      File copied successfully!

In this example, we have made originalfile.txt and created a duplicate named copyfile.txt in the same directory. If the file doesn’t exist, the copy() function will fail and return false.

Example 2: Copy with File Existence Check

<?php
$source = "originalfile.txt";
$destination = "backupfile.txt";

// Checking the file existence.
if (file_exists($source)) {
  // Copying a file.
   if (copy($source, $destination)) {
       echo "File copied successfully!";
   } else {
       echo "Error: Could not copy the file.";
   }
} else {
    echo "Error: Source file does not exist.";
}
?>
Output:
      File copied successfully!

The above PHP script first will check if originalfile.txt exists before attempting to copy it. If the file doesn’t exist, the code will show a clear error message.

Use Cases of File Copying in PHP with Examples


File copying in PHP is a powerful feature that lets you to automate workflows, secure backups data, and organize user data or contents. Let’s explore the most common real-life scenarios where file copying becomes essential.

  • Every web application needs a automatic backup system. Suppose you have a CMS where users constantly upload files. To ensure data safety, you can set up an automatic backup system that copies files to a backup folder every time a new file is uploaded. Thus, you can restore files in case of accidental deletion or corruption.
  • When users upload files through a form, PHP first stores them in a temporary directory. If you don’t move or copy them to a permanent folder, they’ll be lost once the session ends.
  • Web applications generate log files such as error logs, access logs, etc. that grow large over time. Instead of deleting them, you can archive logs by copying them into a separate folder before clearing.
  • In e-commerce or social media platforms, images and videos uploaded by users are often critical. Making a backup copy of media files ensures the data protection.