API15

JInstaller/copyFiles

From Joomla! Documentation

< API15:JInstaller
Revision as of 13:31, 12 May 2013 by JoomlaWikiBot (talk | contribs) (removing red link to edit, no existant pages)

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 files from source directory to the target directory

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax[edit]

copyFiles($files, $overwrite=null)
Parameter Name Default Value Description
$files $files array with filenames
$overwrite null $overwrite True if existing files can be replaced

Returns[edit]

boolean True on success

Defined in[edit]

libraries/joomla/installer/installer.php

Importing[edit]

jimport( 'joomla.installer.installer' );

Source Body[edit]

function copyFiles($files, $overwrite=null)
{
        /*
         * To allow for manual override on the overwriting flag, we check to see if
         * the $overwrite flag was set and is a boolean value.  If not, use the object
         * allowOverwrite flag.
         */
        if (is_null($overwrite) || !is_bool($overwrite)) {
                $overwrite = $this->_overwrite;
        }

        /*
         * $files must be an array of filenames.  Verify that it is an array with
         * at least one file to copy.
         */
        if (is_array($files) && count($files) > 0)
        {
                foreach ($files as $file)
                {
                        // Get the source and destination paths
                        $filesource     = JPath::clean($file['src']);
                        $filedest       = JPath::clean($file['dest']);
                        $filetype       = array_key_exists('type', $file) ? $file['type'] : 'file';

                        if (!file_exists($filesource)) {
                                /*
                                 * The source file does not exist.  Nothing to copy so set an error
                                 * and return false.
                                 */
                                JError::raiseWarning(1, 'JInstaller::install: '.JText::sprintf('File does not exist', $filesource));
                                return false;
                        } elseif (file_exists($filedest) && !$overwrite) {

                                        /*
                                         * It's okay if the manifest already exists
                                         */
                                        if ($this->getPath( 'manifest' ) == $filesource) {
                                                continue;
                                        }

                                        /*
                                         * The destination file already exists and the overwrite flag is false.
                                         * Set an error and return false.
                                         */
                                        JError::raiseWarning(1, 'JInstaller::install: '.JText::sprintf('WARNSAME', $filedest));
                                        return false;
                        } else {

                                // Copy the folder or file to the new location.
                                if ( $filetype == 'folder') {

                                        if (!(JFolder::copy($filesource, $filedest, null, $overwrite))) {
                                                JError::raiseWarning(1, 'JInstaller::install: '.JText::sprintf('Failed to copy folder to', $filesource, $filedest));
                                                return false;
                                        }

                                        $step = array ('type' => 'folder', 'path' => $filedest);
                                } else {

                                        if (!(JFile::copy($filesource, $filedest))) {
                                                JError::raiseWarning(1, 'JInstaller::install: '.JText::sprintf('Failed to copy file to', $filesource, $filedest));
                                                return false;
                                        }

                                        $step = array ('type' => 'file', 'path' => $filedest);
                                }

                                /*
                                 * Since we copied a file/folder, we want to add it to the installation step stack so that
                                 * in case we have to roll back the installation we can remove the files copied.
                                 */
                                $this->_stepStack[] = $step;
                        }
                }
        } else {

                /*
                 * The $files variable was either not an array or an empty array
                 */
                return false;
        }
        return count($files);
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples[edit]

<CodeExamplesForm />