Back to FtpClient class

Method getInstance

public static \Joomla\CMS\Client\FtpClient
getInstance
(mixed $host = '127.0.0.1', mixed $port = '21', array $options = array(), mixed $user = null, mixed $pass = null)
Returns the global FTP connector object, only creating it if it doesn't already exist.
Parameters
  • string $host Host to connect to
  • string $port Port to connect to
  • array $options Array with any of these options: type=>[FTP_AUTOASCII|FTP_ASCII|FTP_BINARY], timeout=>(int)
  • string $user Username to use for a connection
  • string $pass Password to use for a connection
Returns
  • \Joomla\CMS\Client\FtpClient The FTP Client object.
Since
  • 1.5
Class: FtpClient
Project: Joomla

Method getInstance - Source code

/**
 * Returns the global FTP connector object, only creating it
 * if it doesn't already exist.
 *
 * You may optionally specify a username and password in the parameters. If you do so,
 * you may not login() again with different credentials using the same object.
 * If you do not use this option, you must quit() the current connection when you
 * are done, to free it for use by others.
 *
 * @param   string  $host     Host to connect to
 * @param   string  $port     Port to connect to
 * @param   array   $options  Array with any of these options: type=>[FTP_AUTOASCII|FTP_ASCII|FTP_BINARY], timeout=>(int)
 * @param   string  $user     Username to use for a connection
 * @param   string  $pass     Password to use for a connection
 *
 * @return  FtpClient        The FTP Client object.
 *
 * @since   1.5
 */
public static function getInstance($host = '127.0.0.1', $port = '21', array $options = array(), $user = null, $pass = null)
{
    $signature = $user . ':' . $pass . '@' . $host . ':' . $port;
    // Create a new instance, or set the options of an existing one
    if (!isset(static::$instances[$signature]) || !\is_object(static::$instances[$signature])) {
        static::$instances[$signature] = new static($options);
    } else {
        static::$instances[$signature]->setOptions($options);
    }
    // Connect to the server, and login, if requested
    if (!static::$instances[$signature]->isConnected()) {
        $return = static::$instances[$signature]->connect($host, $port);
        if ($return && $user !== null && $pass !== null) {
            static::$instances[$signature]->login($user, $pass);
        }
    }
    return static::$instances[$signature];
}