Computer >> Computer tutorials >  >> Programming >> PHP

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

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

Output

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