copy() function in PHP


The copy() function copies a file. A copy of the source file to the destination file gets created. if the destination file is already present, it gets overwritten.

Syntax

copy(source_file, dest_file)

Parameters

  • source_file − Set the file to copy

  • dest_file − Set the file to copy to

Return

The copy() function returns.

  • TRUE, on success
  • FALSE, on failure

Example

<?php
echo copy("D:/myfiles/sourcefile.dat","D:/myfiles/destfile.dat");
?>

Output

true

Let us see another example now.

Example

 Live Demo

<?php
   $file = '/usr/home/guest/example.txt';
   $newfile = '/usr/home/guest/example.txt.bak';
   if (!copy($file, $newfile)) {
      echo "failed to copy $file...
";    } else {       echo "copied $file into $newfile
";    } ?>

Output

failed to copy /usr/home/guest/example.txt...

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 24-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements