API15:JFolder/copy
From Joomla! Documentation
Description
Copy a folder.
[<! removed edit link to red link >]
<! removed transcluded page call, red link never existed >
Syntax
copy($src, $dest, $path= '', $force=false)
| Parameter Name | Default Value | Description |
|---|---|---|
| $src | The path to the source folder. | |
| $dest | The path to the destination folder. | |
| $path | An optional base path to prefix to the file names. | |
| $force | false | Optionally force folder/file overwrites. |
Returns
mixed object on failure or boolean True on success.
Defined in
libraries/joomla/filesystem/folder.php
Importing
jimport( 'joomla.filesystem.folder' );
Source Body
function copy($src, $dest, $path = '', $force = false)
{
// Initialize variables
jimport('joomla.client.helper');
$ftpOptions = JClientHelper::getCredentials('ftp');
if ($path) {
$src = JPath::clean($path . DS . $src);
$dest = JPath::clean($path . DS . $dest);
}
// Eliminate trailing directory separators, if any
$src = rtrim($src, DS);
$dest = rtrim($dest, DS);
if (!JFolder::exists($src)) {
return JError::raiseError(-1, JText::_('Cannot find source folder'));
}
if (JFolder::exists($dest) && !$force) {
return JError::raiseError(-1, JText::_('Folder already exists'));
}
// Make sure the destination exists
if (! JFolder::create($dest)) {
return JError::raiseError(-1, JText::_('Unable to create target folder'));
}
if ($ftpOptions['enabled'] == 1)
{
// Connect the FTP client
jimport('joomla.client.ftp');
$ftp = &JFTP::getInstance(
$ftpOptions['host'], $ftpOptions['port'], null,
$ftpOptions['user'], $ftpOptions['pass']
);
if (!($dh = @opendir($src))) {
return JError::raiseError(-1, JText::_('Unable to open source folder'));
}
// Walk through the directory copying files and recursing into folders.
while (($file = readdir($dh)) !== false) {
$sfid = $src . DS . $file;
$dfid = $dest . DS . $file;
switch (filetype($sfid)) {
case 'dir':
if ($file != '.' && $file != '..') {
$ret = JFolder::copy($sfid, $dfid, null, $force);
if ($ret !== true) {
return $ret;
}
}
break;
case 'file':
// Translate path for the FTP account
$dfid = JPath::clean(str_replace(JPATH_ROOT, $ftpOptions['root'], $dfid), '/');
if (! $ftp->store($sfid, $dfid)) {
return JError::raiseError(-1, JText::_('Copy failed'));
}
break;
}
}
} else {
if (!($dh = @opendir($src))) {
return JError::raiseError(-1, JText::_('Unable to open source folder'));
}
// Walk through the directory copying files and recursing into folders.
while (($file = readdir($dh)) !== false) {
$sfid = $src . DS . $file;
$dfid = $dest . DS . $file;
switch (filetype($sfid)) {
case 'dir':
if ($file != '.' && $file != '..') {
$ret = JFolder::copy($sfid, $dfid, null, $force);
if ($ret !== true) {
return $ret;
}
}
break;
case 'file':
if (!@copy($sfid, $dfid)) {
return JError::raiseError(-1, JText::_('Copy failed'));
}
break;
}
}
}
return true;
}
[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >
Examples
Code Examples