PHP Tutorial - PHP copy() Function






Definition

The copy() function copies a file.

Syntax

PHP copy() Function has the following syntax.

copy(file,to_file)

Parameter

ParameterIs RequiredDescription
fileRequired.File to copy
to_fileRequired.File to copy to

Note

If the destination file already exists, it will be overwritten.

Return

This function returns TRUE on success and FALSE on failure.





Example 1

Files are copied using copy().


<?php
echo copy("source.txt","target.txt");
?>

Example 2

copy() takes two parameters: the filename you wish to copy from and the filename you wish to copy to.


<?PHP/*from   w  w w .jav  a 2s. co  m*/
      $filename = "c:/abc/test.txt";
      $filename2 = $filename . '.old';
      $result = copy($filename, $filename2);
      if ($result) {
             print "$filename has been copied to $filename2.\n";
      } else {
             print "Error: couldn't copy $filename to $filename2!\n";
      }
?>