note 93953 modified in function.copy by thiago
| From: | thiago@php.net | Date: | Wed, 04 Nov 2009 21:28:19 +0000 |
| Subject: | note 93953 modified in function.copy by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162402@lists.php.net to get a copy of this message | ||
the nicest way of copying files recursivly i found was editing one of the bigger functions below
(thanks for the idea!).
for some reason the old version didnt work for me, which made me rewrite it....
of course one could quite easily add file-permission-recognition to make an exact copy as well as
implement filters to *not* copy every file / folder.
<?php
function smartCopy($source, $dest, $folderPermission=0755,$filePermission=0644){
# source=file & dest=dir => copy file from source-dir to dest-dir
# source=file & dest=file / not there yet => copy file from source-dir to dest and overwrite
a file there, if present
# source=dir & dest=dir => copy all content from source to dir
# source=dir & dest not there yet => copy all content from source to a, yet to be created,
dest-dir
$result=false;
if (is_file($source)) { # $source is file
if(is_dir($dest)) { # $dest is folder
if ($dest[strlen($dest)-1]!='/') # add '/' if necessary
$__dest=$dest."/";
$__dest .= basename($source);
}
else { # $dest is (new) filename
$__dest=$dest;
}
$result=copy($source, $__dest);
chmod($__dest,$filePermission);
}
elseif(is_dir($source)) { # $source is dir
if(!is_dir($dest)) { # dest-dir not there yet, create it
@mkdir($dest,$folderPermission);
chmod($dest,$folderPermission);
}
if ($source[strlen($source)-1]!='/') # add '/' if necessary
$source=$source."/";
if ($dest[strlen($dest)-1]!='/') # add '/' if necessary
$dest=$dest."/";
# find all elements in $source
$result = true; # in case this dir is empty it would otherwise return false
$dirHandle=opendir($source);
while($file=readdir($dirHandle)) { # note that $file can also be a folder
if($file!="." && $file!="..") { # filter starting elements and pass
the rest to this function again
# echo "$source$file ||| $dest$file<br />\n";
$result=smartCopy($source.$file, $dest.$file, $folderPermission, $filePermission);
}
}
closedir($dirHandle);
}
else {
$result=false;
}
return $result;
}
?>
[EDIT BY thiago AT php DOT net: This function has improvements from StephanWilhelm]
--was--
the nicest way of copying files recursivly i found was editing one of the bigger functions below
(thanks for the idea!).
for some reason the old version didnt work for me, which made me rewrite it....
of course one could quite easily add file-permission-recognition to make an exact copy as well as
implement filters to *not* copy every file / folder.
<?php
function smartCopy($source, $dest, $folderPermission=0755,$filePermission=0644){
# source=file & dest=dir => copy file from source-dir to dest-dir
# source=file & dest=file / not there yet => copy file from source-dir to dest and overwrite
a file there, if present
# source=dir & dest=dir => copy all content from source to dir
# source=dir & dest not there yet => copy all content from source to a, yet to be created,
dest-dir
$result=false;
if (is_file($source)) { # $source is file
if(is_dir($dest)) { # $dest is folder
if ($dest[strlen($dest)-1]!='/') # add '/' if necessary
$__dest=$dest."/";
$__dest .= basename($source);
}
else { # $dest is (new) filename
$__dest=$dest;
}
$result=copy($source, $__dest);
chmod($__dest,$filePermission);
}
elseif(is_dir($source)) { # $source is dir
if(!is_dir($dest)) { # dest-dir not there yet, create it
@mkdir($dest,$folderPermission);
chmod($dest,$folderPermission);
}
if ($source[strlen($source)-1]!='/') # add '/' if necessary
$source=$source."/";
if ($dest[strlen($dest)-1]!='/') # add '/' if necessary
$dest=$dest."/";
# find all elements in $source
$return = true; # in case this dir is empty it would otherwise return false
$dirHandle=opendir($source);
while($file=readdir($dirHandle)) { # note that $file can also be a folder
if($file!="." && $file!="..") { # filter starting elements and pass
the rest to this function again
# echo "$source$file ||| $dest$file<br />\n";
$result=smartCopy($source.$file, $dest.$file, $folderPermission, $filePermission);
}
}
closedir($dirHandle);
}
else {
$result=false;
}
return $result;
}
?>
http://php.net/manual/en/function.copy.php