public static bool
copy
(mixed $src, mixed $dest, mixed $path = '', mixed $force = false, mixed $useStreams = false)
/**
* Copy a folder.
*
* @param string $src The path to the source folder.
* @param string $dest The path to the destination folder.
* @param string $path An optional base path to prefix to the file names.
* @param boolean $force Force copy.
* @param boolean $useStreams Optionally force folder/file overwrites.
*
* @return boolean True on success.
*
* @since 1.7.0
* @throws \RuntimeException
*/
public static function copy($src, $dest, $path = '', $force = false, $useStreams = false)
{
@set_time_limit(ini_get('max_execution_time'));
$FTPOptions = ClientHelper::getCredentials('ftp');
if ($path) {
$src = Path::clean($path . '/' . $src);
$dest = Path::clean($path . '/' . $dest);
}
// Eliminate trailing directory separators, if any
$src = rtrim($src, DIRECTORY_SEPARATOR);
$dest = rtrim($dest, DIRECTORY_SEPARATOR);
if (!self::exists($src)) {
throw new \RuntimeException('Source folder not found', -1);
}
if (self::exists($dest) && !$force) {
throw new \RuntimeException('Destination folder already exists', -1);
}
// Make sure the destination exists
if (!self::create($dest)) {
throw new \RuntimeException('Cannot create destination folder', -1);
}
// If we're using ftp and don't have streams enabled
if ($FTPOptions['enabled'] == 1 && !$useStreams) {
// Connect the FTP client
$ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
if (!($dh = @opendir($src))) {
throw new \RuntimeException('Cannot open source folder', -1);
}
// Walk through the directory copying files and recursing into folders.
while (($file = readdir($dh)) !== false) {
$sfid = $src . '/' . $file;
$dfid = $dest . '/' . $file;
switch (filetype($sfid)) {
case 'dir':
if ($file != '.' && $file != '..') {
$ret = self::copy($sfid, $dfid, null, $force);
if ($ret !== true) {
return $ret;
}
}
break;
case 'file':
// Translate path for the FTP account
$dfid = Path::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dfid), '/');
if (!$ftp->store($sfid, $dfid)) {
throw new \RuntimeException('Copy file failed', -1);
}
break;
}
}
} else {
if (!($dh = @opendir($src))) {
throw new \RuntimeException('Cannot open source folder', -1);
}
// Walk through the directory copying files and recursing into folders.
while (($file = readdir($dh)) !== false) {
$sfid = $src . '/' . $file;
$dfid = $dest . '/' . $file;
switch (filetype($sfid)) {
case 'dir':
if ($file != '.' && $file != '..') {
$ret = self::copy($sfid, $dfid, null, $force, $useStreams);
if ($ret !== true) {
return $ret;
}
}
break;
case 'file':
if ($useStreams) {
$stream = Factory::getStream();
if (!$stream->copy($sfid, $dfid)) {
throw new \RuntimeException(sprintf("Cannot copy file: %s", Path::removeRoot($stream->getError())), -1);
}
} else {
if (!@copy($sfid, $dfid)) {
throw new \RuntimeException('Copy file failed', -1);
}
}
break;
}
}
}
return true;
}