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", "geeksforgeeks.txt");
Output : true

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

Below programs illustrate the copy() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

File has been copied!

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



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.