note 93953 added to function.copy
| From: | info | Date: | Thu, 08 Oct 2009 10:10:38 +0000 |
| Subject: | note 93953 added to function.copy | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161502@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.
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;
}
----
Server IP: 217.160.72.57
Probable Submitter: 77.186.123.171
----
Manual Page -- http://www.php.net/manual/en/function.copy.php
Edit -- https://master.php.net/note/edit/93953
Del: integrated -- https://master.php.net/note/delete/93953/integrated
Del: useless -- https://master.php.net/note/delete/93953/useless
Del: bad code -- https://master.php.net/note/delete/93953/bad+code
Del: spam -- https://master.php.net/note/delete/93953/spam
Del: non-english -- https://master.php.net/note/delete/93953/non-english
Del: in docs -- https://master.php.net/note/delete/93953/in+docs
Del: other reasons-- https://master.php.net/note/delete/93953
Reject -- https://master.php.net/note/reject/93953
Search -- https://master.php.net/manage/user-notes.php