PHP: copy - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

delete clearstatcache

Last updated: Sat, 27 Jan 2007

view this page in

# copy

(PHP 3, PHP 4, PHP 5)

copy -- Копирует файл

## Описание

bool copy ( string source, string dest )

Создает копию файла, чье имя передано в параметре `source`, в файле с именем `dest`. Возвращает `TRUE` в случае успешного завершения или `FALSE` в случае возникновения ошибки.

Пример 1. Пример использования функции copy()

`<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';
if (!copy($file, $newfile)) {
echo "не удалось скопировать $file...\n";
}
?>`

Если вы хотите переместить файл, воспользуйтесь функцией rename().

> Замечание: Начиная с PHP версии 4.3.0, оба параметра, `source` и `dest`, могут быть URL'ами, если были включены "упаковщики fopen". За более подробной информацией обратитесь к описанию функции fopen(). Если параметр `dest` является URL, выполнение функции может завершиться ошибкой, если упаковщик не поддерживает перезапись существующих файлов.

Внимание
Если файл-назначение существует, он будет перезаписан.

> Замечание: Замечание отностильно совместимости Windows: Если вы копируете пустой файл, copy() вернёт `FALSE`, но файл будет нормально скопирован.

См.также описание функций move_uploaded_file(), rename(), а также главу руководства Обработка загрузки файлов.

add a note User Contributed Notes
copy
jtaylor -at- ashevillenc -dot- com
13-Feb-2007 12:48
`It seems as though you can only use move_uploaded_file() once on a temporary file that has been uploaded through a form. Most likely the action this function takes destroys the temporary file after it has been moved, assuming permanent placement on a file system.
Attempting to use the function again in the same PHP script will return false and not move the file.
I ran into this when an image that was uploaded did not need resizing (smaller than a size threshold) and after moving the temporary upload to the "originals" directory, an attempt was made to again move the temporary file to another folder.
This behavior is understandable, but be careful - in this instance, I simply used copy() on the file that was already uploaded.`
mspreij
27-Jan-2007 08:35
`hello editor, could you please fix my previous post..
# preg hereunder matches anything with at least one period in the middle (that is,
# non-period characters on either side), and an extension of 1-5 characters
changes to
# preg hereunder matches anything with at least one period in the middle and an extension of 1-5 characters
Sorry!`
mspreij
27-Jan-2007 08:20
`This function creates a new filename to use for a copy of the given filename, its behaviour was mostly sto^Wborrowed from how the OS X Finder (*1) does it.
Note it *doesn't* actually copy the file, it just returns the new name. I needed it to work regardless of data source (filesystem, ftp, etc).
It also tries to match the current name as neatly as possible:
foo.txt -> foo copy.txt -> foo copy 1.txt -> foo copy 2.txt [etc]
foo.bar.baz.jpg -> foo.bar.baz copy.jpg
foobar -> foobar copy -> foobar copy 1 [etc]
".txt" -> .txt copy, and "txt." -> txt. copy
file.longextension -> file.longextension copy
It keeps trying until it finds a name that is not yet taken in $list, or until it looped 500 times (change as needed).
If the renamed file becomes longer than max filename length, it starts chopping away at the end of the part before where it adds " copy": reallylong...filename.txt -> reallylong...filena copy.txt
<?php
// $orig = current name, of course
// $list = array of filenames in the target directory (if none given, it will still return a new name)
// $max = max length of filename
function duplicate_name($orig, $list = array(), $max = 64) {
$ext = '';
$counter = 0;
$list = (array) $list;
$max = (int) $max;
$newname = $orig;
do {
$name = $newname; # name in, newname out
if (preg_match('/ copy$| copy \d+$/', $name, $matches)) {
// don't even check for extension, name ends with " copy[ digits]"
// preg hereunder matches anything with at least one period in the middle (that is,
// non-period characters on either side), and an extension of 1-5 characters
}elseif (preg_match('/(.+)\.([^.]{1,5})$/', $name, $parts)) {
// split to name & extension
list($name, $ext) = array($parts[1], $parts[2]);
}
if (preg_match('/ copy (\d+)$/', $name, $digits)) {
$newname = substr($name, 0, - strlen($digits[1])) . ($digits[1] + 1);
# $cutlen is only used for the bit at the end where it checks on max filename length
$cutlen = 6 + strlen($digits[1]+1); // ' copy ' + digits
}elseif(preg_match('/ copy$/', $name, $digits)) {
$newname = $name . ' 1';
$cutlen = 7; // ' copy' + ' 1'
}else{
$newname = $name . ' copy';
$cutlen = 5; // ' copy'
}
if ($ext) {
$newname .= '.' . $ext;
$cutlen += strlen($ext) + 1;
}
if ($max > 0) {
if (strlen($newname) > $max) {
$newname = substr($newname, 0, max($max - $cutlen, 0)) . substr($newname, -$cutlen);
if (strlen($newname) > $max) {echo "duplicate_name() error: Can't keep the new name under given max length.\n"; return false;}
}
}
if ($counter++ > 500) {echo "duplicate_name() error: Too many similarly named files or infinite while loop.\n"; return false;}
} while (in_array($newname, $list));
return $newname;
}
?>
*1) The Finder seems to check the extension vs a list of known extensions, this function considers it valid if it's 5 or fewer characters long.
ps. sorry for taking up so much space! :-)`
Belandi
13-Dec-2006 05:09
`To use the previous function on UNIX systems you should change these lines:
$srcfile = $srcdir . '\\' . $file;
$dstfile = $dstdir . '\\' . $file;
To match the following:
$srcfile = $srcdir . DIRECTORY_SEPARATOR . $file;
$dstfile = $dstdir . DIRECTORY_SEPARATOR . $file;`
SkyEye
07-Oct-2006 07:21
`<?php
// A function to copy files from one directory to another one, including subdirectories and
// nonexisting or newer files. Function returns number of files copied.
// This function is PHP implementation of Windows xcopy A:\dir1\* B:\dir2 /D /E /F /H /R /Y
// Syntaxis: [$number =] dircopy($sourcedirectory, $destinationdirectory [, $verbose]);
// Example: $num = dircopy('A:\dir1', 'B:\dir2', 1);
function dircopy($srcdir, $dstdir, $verbose = false) {
$num = 0;
if(!is_dir($dstdir)) mkdir($dstdir);
if($curdir = opendir($srcdir)) {
while($file = readdir($curdir)) {
if($file != '.' && $file != '..') {
$srcfile = $srcdir . '\\' . $file;
$dstfile = $dstdir . '\\' . $file;
if(is_file($srcfile)) {
if(is_file($dstfile)) $ow = filemtime($srcfile) - filemtime($dstfile); else $ow = 1;
if($ow > 0) {
if($verbose) echo "Copying '$srcfile' to '$dstfile'...";
if(copy($srcfile, $dstfile)) {
touch($dstfile, filemtime($srcfile)); $num++;
if($verbose) echo "OK\n";
}
else echo "Error: File '$srcfile' could not be copied!\n";
}
}
else if(is_dir($srcfile)) {
$num += dircopy($srcfile, $dstfile, $verbose);
}
}
}
closedir($curdir);
}
return $num;
}
?>`
fred dot haab at gmail dot com
18-Aug-2006 05:49
`Just some caveats for people to consider when working with this copy function. Like everyone else, I needed a recursive copy function, so I wrote one.
It worked great until I hit a file greater than 2GB, at which point I got a File Size Exceeded error. I've read that you can recompile PHP with larger file support, but I'm not able to do this on my system.
So I used exec to copy one file at a time (I need to do other things to the file, so can't just do an exec with "cp -r"). Then I ran into problems with spaces in the names. The escapeshellcmd and escapeshellargs functions don't seem to cleanup spaces, so I wrote my own function to do that.`
denis at i39 dot ru
10-Aug-2006 05:53
`You can also try to copy with:
<?
exec("cp -r /var/www/mysite /var/backup");
?>`
SBoisvert at Don'tSpamMe dot Bryxal dot ca
25-Jul-2006 01:38
`to the editor's: Please pardon me.... remove my other 2 messages and this little comment and just stick this.
just a quick note to add to the great function by:bobbfwed at comcast dot net
this little version has the path not in a define but as a function parameter and also creates the destination directory if it is not already created.
<?php
// copy a directory and all subdirectories and files (recursive)
// void dircpy( str 'source directory', str 'destination directory' [, bool 'overwrite existing files'] )
function dircpy($basePath, $source, $dest, $overwrite = false){
if(!is_dir($basePath . $dest)) //Lets just make sure our new folder is already created. Alright so its not efficient to check each time... bite me
mkdir($basePath . $dest);
if($handle = opendir($basePath . $source)){ // if the folder exploration is sucsessful, continue
while(false !== ($file = readdir($handle))){ // as long as storing the next file to $file is successful, continue
if($file != '.' && $file != '..'){
$path = $source . '/' . $file;
if(is_file($basePath . $path)){
if(!is_file($basePath . $dest . '/' . $file) || $overwrite)
if(!@copy($basePath . $path, $basePath . $dest . '/' . $file)){
echo '<font color="red">File ('.$path.') could not be copied, likely a permissions problem.</font>';
}
} elseif(is_dir($basePath . $path)){
if(!is_dir($basePath . $dest . '/' . $file))
mkdir($basePath . $dest . '/' . $file); // make subdirectory before subdirectory is copied
dircpy($basePath, $path, $dest . '/' . $file, $overwrite); //recurse!
}
}
}
closedir($handle);
}
}
?>`
emielm at hotmail dot com
08-Jul-2006 12:49
`I have been puzzling for hours with the copy() function. I got a "no such file or directory" error message all the time (for the source file). In the end my mistake was that there were some spaces at the end of de source filename...
So, if you get file not found errors and you are sure that the file does exists, use the trim() function to get rid of the spaces.`
cooper at asu dot ntu-kpi dot kiev dot ua
09-Mar-2006 04:32
`It take me a long time to find out what the problem is when i've got an error on copy(). It DOESN'T create any directories. It only copies to existing path. So create directories before. Hope i'll help,`
bobbfwed at comcast dot net
02-Feb-2006 11:06
`I have programmed a really nice program that remotely lets you manage files as if you have direct access to them (http://sourceforge.net/projects/filemanage/). I have a bunch of really handy functions to do just about anything to files or directories.
I know there are others like it, but here is the function I made for this program to copy directories; it will likely need tweaking to work as a standalone script, since it relies of variables set by my program (eg: loc1 -- which dynamically changes in my program):
<?PHP
// loc1 is the path on the computer to the base directory that may be moved
define('loc1', 'C:/Program Files/Apache Group/Apache/htdocs', true);
// copy a directory and all subdirectories and files (recursive)
// void dircpy( str 'source directory', str 'destination directory' [, bool 'overwrite existing files'] )
function dircpy($source, $dest, $overwrite = false){
if($handle = opendir(loc1 . $source)){ // if the folder exploration is sucsessful, continue
while(false !== ($file = readdir($handle))){ // as long as storing the next file to $file is successful, continue
if($file != '.' && $file != '..'){
$path = $source . '/' . $file;
if(is_file(loc1 . $path)){
if(!is_file(loc1 . $dest . '/' . $file) || $overwrite)
if(!@copy(loc1 . $path, loc1 . $dest . '/' . $file)){
echo '<font color="red">File ('.$path.') could not be copied, likely a permissions problem.</font>';
}
} elseif(is_dir(loc1 . $path)){
if(!is_dir(loc1 . $dest . '/' . $file))
mkdir(loc1 . $dest . '/' . $file); // make subdirectory before subdirectory is copied
dircpy($path, $dest . '/' . $file, $overwrite); //recurse!
}
}
}
closedir($handle);
}
} // end of dircpy()
?>
This new function will be in 0.9.7 (the current release of File Manage) which has just been released 2/2/06.
Hope this helps some people.`
makarenkoa at ukrpost dot net
26-Jul-2005 04:58
`A function that copies contents of source directory to destination directory and sets up file modes.
It may be handy to install the whole site on hosting.
<?php
// copydirr.inc.php
/*
26.07.2005
Author: Anton Makarenko
makarenkoa at ukrpost dot net
webmaster at eufimb dot edu dot ua
*/
function copydirr($fromDir,$toDir,$chmod=0757,$verbose=false)
/*
copies everything from directory $fromDir to directory $toDir
and sets up files mode $chmod
*/
{
//* Check for some errors
$errors=array();
$messages=array();
if (!is_writable($toDir))
$errors[]='target '.$toDir.' is not writable';
if (!is_dir($toDir))
$errors[]='target '.$toDir.' is not a directory';
if (!is_dir($fromDir))
$errors[]='source '.$fromDir.' is not a directory';
if (!empty($errors))
{
if ($verbose)
foreach($errors as $err)
echo '<strong>Error</strong>: '.$err.'<br />';
return false;
}
//*/
$exceptions=array('.','..');
//* Processing
$handle=opendir($fromDir);
while (false!==($item=readdir($handle)))
if (!in_array($item,$exceptions))
{
//* cleanup for trailing slashes in directories destinations
$from=str_replace('//','/',$fromDir.'/'.$item);
$to=str_replace('//','/',$toDir.'/'.$item);
//*/
if (is_file($from))
{
if (@copy($from,$to))
{
chmod($to,$chmod);
touch($to,filemtime($from)); // to track last modified time
$messages[]='File copied from '.$from.' to '.$to;
}
else
$errors[]='cannot copy file from '.$from.' to '.$to;
}
if (is_dir($from))
{
if (@mkdir($to))
{
chmod($to,$chmod);
$messages[]='Directory created: '.$to;
}
else
$errors[]='cannot create directory '.$to;
copydirr($from,$to,$chmod,$verbose);
}
}
closedir($handle);
//*/
//* Output
if ($verbose)
{
foreach($errors as $err)
echo '<strong>Error</strong>: '.$err.'<br />';
foreach($messages as $msg)
echo $msg.'<br />';
}
//*/
return true;
}
/* sample usage:
WARNING:
if You set wrong $chmod then You'll not be able to access files and directories
in destination directory.
For example: once upon a time I've called the function with parameters:
copydir($fromDir,$toDir,true);
What happened? I've forgotten one parameter (chmod)
What happened next? Those files and directories became inaccessible for me
(they had mode 0001), so I had to ask sysadmin to delete them from root account
Be careful :-)
<?php
require('./copydirr.inc.php');
copydirr('./testSRC','D:/srv/Apache2/htdocs/testDEST',0777,true);
?>
*/
?>`
Andrzej Nadziejko (andrzej at vao . pl)
27-Jun-2005 12:52
`These functions are to copy and remove big directories:
/*
source files are in source directory
*/
function SetupFolder($dir_name)
{
mkdir($dir_name,'0777');
$folder = opendir('source');
while($file = readdir($folder))
{
if ($file == '.' || $file == '..') {
continue;
}
if(is_dir('source/'.$file))
{
mkdir($dir_name.'/'.$file,0777);
CopyFiles('source/'.$file,$dir_name.'/'.$file);
}
else
{
copy('source/'.$file,$dir_name.'/'.$file);
}
}
closedir($folder);
return 1;
}
//copy many files
function CopyFiles($source,$dest)
{
$folder = opendir($source);
while($file = readdir($folder))
{
if ($file == '.' || $file == '..') {
continue;
}
if(is_dir($source.'/'.$file))
{
mkdir($dest.'/'.$file,0777);
CopySourceFiles($source.'/'.$file,$dest.'/'.$file);
}
else
{
copy($source.'/'.$file,$dest.'/'.$file);
}
}
closedir($folder);
return 1;
}
//remove file, directories, subdirectories
function RemoveFiles($source)
{
$folder = opendir($source);
while($file = readdir($folder))
{
if ($file == '.' || $file == '..') {
continue;
}
if(is_dir($source.'/'.$file))
{
RemoveFiles($source.'/'.$file);
}
else
{
unlink($source.'/'.$file);
}
}
closedir($folder);
rmdir($source);
return 1;
}`
info at sameprecision dot org
28-Jan-2005 03: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 04: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 01: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 12:29
`If you want to copy a file, or an entire folder (including the contents), use the below function.
http://aidanlister.com/repos/v/function.copyr.php`
kadnan at yahoo dot com
29-Aug-2004 10: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`
add a note
delete clearstatcache

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST