What is the copy function in PHP?

PHP offers many functions for file manipulation. The copy function copies the contents of one file to another.

Syntax

The syntax for the copy function is shown below:

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

Parameters

The copy function takes three parameters:

  • source: This is a required parameter. source is the path of the file to be copied to the other file.
  • destination: This is a required parameter. destination is the path of the file to which content from the source file will be written.
  • context: This is an optional parameter. context specifies a context resource created with stream_context_create(). The context is determined by what is being passed along (streamed); this means that the situation is unique for different data types.

If the file already has content in it, it will be overwritten when it is used as the destination file.

Return value

The function returns a bool value; 1 or true upon success, and 0 or false in case of errors or failure.

The working of copy function.

Code

The following code shows how the copy function works.

main.php
destination.txt
source.txt
Hello,
World!

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved