Difference between revisions of "JFolder/copy"
From Joomla! Documentation
< API15:JFolder
(New page: ===Description===
Copy a folder.
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>Edit Descripton<nowiki>]</nowiki>
</span>
{{Descr...) |
m (removing red link to edit, no existant pages) |
||
Line 3: | Line 3: | ||
<span class="editsection" style="font-size:76%;"> | <span class="editsection" style="font-size:76%;"> | ||
− | <nowiki>[< | + | <nowiki>[<! removed edit link to red link >]</nowiki> |
</span> | </span> | ||
− | + | <! removed transcluded page call, red link never existed > | |
===Syntax=== | ===Syntax=== | ||
Line 137: | Line 137: | ||
<span class="editsection" style="font-size:76%;"> | <span class="editsection" style="font-size:76%;"> | ||
− | <nowiki>[< | + | <nowiki>[<! removed edit link to red link >]</nowiki> |
</span> | </span> | ||
− | + | <! removed transcluded page call, red link never existed > | |
===Examples=== | ===Examples=== | ||
Line 152: | Line 152: | ||
format= ,,, | format= ,,, | ||
</dpl> | </dpl> | ||
+ | [[Category:Archived pages API15]] |
Revision as of 12:19, 12 May 2013
The "API15" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.
Description[edit]
Copy a folder.
[<! removed edit link to red link >]
<! removed transcluded page call, red link never existed >
Syntax[edit]
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[edit]
mixed object on failure or boolean True on success.
Defined in[edit]
libraries/joomla/filesystem/folder.php
Importing[edit]
jimport( 'joomla.filesystem.folder' );
Source Body[edit]
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[edit]
<CodeExamplesForm />