Thursday, September 4, 2025
HomeLanguagesPHP copy( ) Function

PHP copy( ) Function

The copy() function in PHP is an inbuilt function which is used to make a copy of a specified file. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure.

Syntax:

bool copy ( $source, $dest )

Parameters: The copy() function in PHP accepts two parameters which are source and destination.

  1. $source: It specifies the path to the source file.
  2. $dest: It is used to specify the path to the destination file.

Return Value: It returns true on success and false on failure.

Errors And Exception:

  1. The copy() function in PHP doesn’t works for remote files.It only works on files which are accessible by the server’s filesystem.
  2. If the destination file already exists, it gets overwritten.

Examples:

Input : echo copy("gfg.txt", "neveropen.txt");
Output : true

Input : $srcfile = '/user01/Desktop/admin/gfg.txt';
        $destfile = 'user01/Desktop/admin/neveropen.txt';
        echo copy($srcfile, $destfilefile);
Output : true

Below programs illustrate the copy() function:

Program 1:




<?php
  
// Copying gfg.txt to neveropen.txt
echo copy("gfg.txt", "neveropen.txt");
  
?>


Output:

true

Program 2:




<?php
  
// Copying gfg.txt to neveropen.txt
$srcfile = '/user01/Desktop/admin/gfg.txt';
$destfile = 'user01/Desktop/admin/neveropen.txt';
  
if (!copy($srcfile, $destfilefile)) {
    echo "File cannot be copied! \n";
}
else {
    echo "File has been copied!";
}
  
?>


Output:

File has been copied!

Reference:
http://php.net/manual/en/function.copy.php

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6632 POSTS0 COMMENTS
Nicole Veronica
11800 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS