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// w  w w  . j a  va2 s  . c  o  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";
      }
?>




















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions