search for in the  
<clearstatcachedelete>
Last updated: Thu, 19 May 2005

copy

(PHP 3, PHP 4, PHP 5)

copy -- Copies file

Description

bool copy ( string source, string dest )

Makes a copy of the file source to dest. Returns TRUE on success or FALSE on failure.

Example 1. copy() example

<?php
$file
= 'example.txt';
$newfile = 'example.txt.bak';

if (!
copy($file, $newfile)) {
   echo
"failed to copy $file...\n";
}
?>

If you wish to move a file, use the rename() function.

Note: As of PHP 4.3.0, both source and dest may be URLs if the "fopen wrappers" have been enabled. See fopen() for more details. If dest is a URL, the copy operation may fail if the wrapper does not support overwriting of existing files.

Warning

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

Note: Windows compatibility: If you copy a file with no size, copy() will return FALSE, but the file will be correctly copied.

See also move_uploaded_file(), rename(), and the section of the manual about handling file uploads.



User Contributed Notes
copy
info at sameprecision dot org
28-Jan-2005 05:54
When using copy on win32 (don't know about anywhere else),  copy sets the 'last modified time' of the the new copy to the current time instead of that of the source (win32 copy preserves last modified time).  If you are tracking backups by last modified time, use:

<?php

copy
($src, $dest); touch($dest, filemtime($src));

?>
20-Jan-2005 06:34
Heres a quick function I wrote to backup whole websites.
I haven't actually tested it out yet. I will later on  =P
<?

function backup($extension) {
  
$counter = 0;
   foreach(
glob(*. “$extension”) as $file) {
       if (
$extension{0} != .) {
          
$extension = .. $extension;
       }
      
$file2 = ./backup/. “$file”;
      
$counter++;
      
copy($file, $file2);
   }
   if (
$counter == 0) {
       return
false;
   } else {
       return
true;
   }
}

/* Test -- Copy all images */

$extension_array = array(.jpg”, .gif”, .png”, .bmp”);

foreach(
$extension_array as $key => $value) {
  
backup($extension);
}

?>
simonr_at_orangutan_dot_co_dot_uk
03-Sep-2004 03:54
Having spent hours tacking down a copy() error: Permission denied , (and duly worrying about chmod on winXP) , its worth pointing out that the 'destination' needs to contain the actual file name ! --- NOT just the path to the folder you wish to copy into.......
DOH !
hope this saves somebody hours of fruitless debugging
aidan at php dot net
30-Aug-2004 02:29
If you want to copy a file, or an entire folder (including the contents), use the below function.

http://aidan.dotgeek.org/lib/?file=function.copyr.php
kadnan at yahoo dot com
30-Aug-2004 12:30
you can also try xcopy command by using Shell to move/copy files/folders from one place to another
here is the code:

<?php
exec
('xcopy c:\\myfolder d:\\myfolder /e/i', $a, $a1);
?>

by executing this command, it will move folder along with all contents to destination.

-adnan

<clearstatcachedelete>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT