API16:JFolder/copy
From Joomla! Documentation
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.
Contents |
Description
Copy a folder.
Syntax
copy($src, $dest, $path= '', $force=false, $use_streams=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. |
| $use_streams | false |
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, $use_streams=false) { // Initialise 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 we're using ftp and don't have streams enabled if ($FTPOptions['enabled'] == 1 && !$use_streams) { // 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, $use_streams); if ($ret !== true) { return $ret; } } break; case 'file': if($use_streams) { $stream =& JFactory::getStream(); if(!$stream->copy($sfid, $dfid)) { return JError::raiseError(-1, JText::_('COPY_FAILED').': '. $stream->getError()); } } else { if (!@copy($sfid, $dfid)) { return JError::raiseError(-1, JText::_('COPY_FAILED')); } } break; } } } return true; }
[Edit See Also] SeeAlso:JFolder/copy
Examples
<CodeExamplesForm />
