/**
* Method to connect to a FTP server
*
* @param string $host Host to connect to [Default: 127.0.0.1]
* @param int $port Port to connect on [Default: port 21]
*
* @return boolean True if successful
*
* @since 3.0.0
*/
public function connect($host = '127.0.0.1', $port = 21)
{
$errno = null;
$err = null;
// If already connected, return
if (\is_resource($this->_conn)) {
return true;
}
// If native FTP support is enabled let's use it...
if (FTP_NATIVE) {
$this->_conn = @ftp_connect($host, $port, $this->_timeout);
if ($this->_conn === false) {
Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_NO_CONNECT', __METHOD__, $host, $port), Log::WARNING, 'jerror');
return false;
}
// Set the timeout for this connection
ftp_set_option($this->_conn, FTP_TIMEOUT_SEC, $this->_timeout);
return true;
}
// Connect to the FTP server.
$this->_conn = @fsockopen($host, $port, $errno, $err, $this->_timeout);
if (!$this->_conn) {
Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_NO_CONNECT_SOCKET', __METHOD__, $host, $port, $errno, $err), Log::WARNING, 'jerror');
return false;
}
// Set the timeout for this connection
socket_set_timeout($this->_conn, $this->_timeout, 0);
// Check for welcome response code
if (!$this->_verifyResponse(220)) {
Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_NOT_EXPECTED_RESPONSE', __METHOD__, $this->_response, 220), Log::WARNING, 'jerror');
return false;
}
return true;
}