I created this function and I'm unsure if it is sufficient for "real world use", because all code snippets on the net I found for recursive copy functions used functions like
$hDir = opendir($sSrc)
which I don't think are necessary. That's why I like to have my func checked by some PHP professionals:
It recursively copies directories.
Params:
$sSrc
Path of directory, of which the content is to be copied$sDst
Path of directory, into which the content of$sSrc
is to be
copied. Non existing directories are (recursively) created.$blnKeepPerm
Optional argument specifying if destination objects
must have the same permission as their source objects
Returns: True only if ALL elements are copied, otherwise false.
function rcopy ($sSrc, $sDst, $blnKeepPerm = 0) {
if (! is_dir($sSrc)) return false;
$sSrc = (substr($sSrc,-1)=='/' ? substr($sSrc,0,-1) : $sSrc);
$sDst = (substr($sDst,-1)=='/' ? substr($sDst,0,-1) : $sDst);
$blnRetval = true;
if (! is_dir($sDst)) {
if ($blnKeepPerm) {
if (! @mkdir($sDst, fileperms($sSrc), true)) return false;
} else {
if (! @mkdir($sDst), (0777-umask()), true) return false;
}
}
foreach (scandir($sSrc) as $sObj) {
if ($sObj != '.' && $sObj != '..') {
if (is_dir("$sSrc/$sObj")) {
// the function returns true only if copying ALL of the
// files/dirs succeeded. To prevent overwriting of $blnRetVal
// by a later rcopy-call, the return values here are ANDed:
$blnRetval = $blnRetval & rcopy ("$sSrc/$sObj", "$sDst/$sObj", $blnKeepPerm);
} else {
if (@copy ("$sSrc/$sObj", "$sDst/$sObj")) {
if ($blnKeepPerm) {
@chmod ("$sDst/$sObj", fileperms("$sSrc/$sObj"));
}
} else $blnRetval = false;
}
}
}
return $blnRetval;
}
What do you think? What could/should be made better?