API16

JFolder/copy

From Joomla! Documentation

< API16:JFolder
Revision as of 17:47, 22 March 2010 by Doxiki (talk | contribs) (New page: ===Description=== Copy a folder. <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>Edit Descripton<nowiki>]</nowiki> </span> {{Descr...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The "API16" 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.

[Edit Descripton]

Template:Description:JFolder/copy

Syntax[edit]

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[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, $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] Template:SeeAlso:JFolder/copy

Examples[edit]

<CodeExamplesForm />