Back to File class

Method copy

public static bool
copy
(mixed $src, mixed $dest, mixed $path = null, mixed $useStreams = false)
Copies a file
Parameters
  • string $src The path to the source file
  • string $dest The path to the destination file
  • string $path An optional base path to prefix to the file names
  • bool $useStreams True to use streams
Returns
  • bool True on success
Since
  • 1.7.0
Class: File
Project: Joomla

Method copy - Source code

/**
 * Copies a file
 *
 * @param   string   $src         The path to the source file
 * @param   string   $dest        The path to the destination file
 * @param   string   $path        An optional base path to prefix to the file names
 * @param   boolean  $useStreams  True to use streams
 *
 * @return  boolean  True on success
 *
 * @since   1.7.0
 */
public static function copy($src, $dest, $path = null, $useStreams = false)
{
    // Prepend a base path if it exists
    if ($path) {
        $src = Path::clean($path . '/' . $src);
        $dest = Path::clean($path . '/' . $dest);
    }
    // Check src path
    if (!is_readable($src)) {
        Log::add(Text::sprintf('LIB_FILESYSTEM_ERROR_JFILE_FIND_COPY', __METHOD__, $src), Log::WARNING, 'jerror');
        return false;
    }
    if ($useStreams) {
        $stream = Factory::getStream();
        if (!$stream->copy($src, $dest)) {
            Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_FILE_STREAMS', __METHOD__, $src, $dest, $stream->getError()), Log::WARNING, 'jerror');
            return false;
        }
        self::invalidateFileCache($dest);
        return true;
    } else {
        $FTPOptions = ClientHelper::getCredentials('ftp');
        if ($FTPOptions['enabled'] == 1) {
            // Connect the FTP client
            $ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
            // If the parent folder doesn't exist we must create it
            if (!file_exists(\dirname($dest))) {
                Folder::create(\dirname($dest));
            }
            // Translate the destination path for the FTP account
            $dest = Path::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
            if (!$ftp->store($src, $dest)) {
                // FTP connector throws an error
                return false;
            }
            $ret = true;
        } else {
            if (!@copy($src, $dest)) {
                Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_COPY_FAILED_ERR01', $src, $dest), Log::WARNING, 'jerror');
                return false;
            }
            $ret = true;
        }
        self::invalidateFileCache($dest);
        return $ret;
    }
}