Back to File class

Method move

public static bool
move
(mixed $src, mixed $dest, mixed $path = '', mixed $useStreams = false)
Moves 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 move - Source code

/**
 * Moves 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 move($src, $dest, $path = '', $useStreams = false)
{
    if ($path) {
        $src = Path::clean($path . '/' . $src);
        $dest = Path::clean($path . '/' . $dest);
    }
    // Check src path
    if (!is_readable($src)) {
        Log::add(Text::_('JLIB_FILESYSTEM_CANNOT_FIND_SOURCE_FILE'), Log::WARNING, 'jerror');
        return false;
    }
    if ($useStreams) {
        $stream = Factory::getStream();
        if (!$stream->move($src, $dest)) {
            Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_GENERIC', __METHOD__, $stream->getError()), Log::WARNING, 'jerror');
            return false;
        }
        self::invalidateFileCache($dest);
        return true;
    } else {
        $FTPOptions = ClientHelper::getCredentials('ftp');
        // Invalidate the compiled OPCache of the old file so it's no longer used.
        self::invalidateFileCache($src);
        if ($FTPOptions['enabled'] == 1) {
            // Connect the FTP client
            $ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
            // Translate path for the FTP account
            $src = Path::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $src), '/');
            $dest = Path::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
            // Use FTP rename to simulate move
            if (!$ftp->rename($src, $dest)) {
                Log::add(Text::_('JLIB_FILESYSTEM_ERROR_RENAME_FILE'), Log::WARNING, 'jerror');
                return false;
            }
        } else {
            if (!@rename($src, $dest)) {
                Log::add(Text::_('JLIB_FILESYSTEM_ERROR_RENAME_FILE'), Log::WARNING, 'jerror');
                return false;
            }
        }
        self::invalidateFileCache($dest);
        return true;
    }
}