Actions

API15

JFolder/copy

From Joomla! Documentation

< API15:JFolder
Revision as of 12:19, 12 May 2013 by JoomlaWikiBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
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.

[<! 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

<CodeExamplesForm />