Oihana Php Ftp

FtpConnectionTrait uses \oihana\logging\LoggerTrait

Provides the connection lifecycle of the {@see \oihana\ftp\FtpClient}: configuration, connect/login with retry, passive mode, secure transport and clean teardown.

All transport calls go through an injected FtpDriverInterface, so the whole lifecycle is testable without a live server.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Properties

$maxRetries  : int
The maximum number of attempts for a transient (connection) failure.
$connected  : bool
Whether the client is currently connected.
$credentials  : FtpCredentials
The login credentials.
$driver  : FtpDriverInterface
The transport driver.
$host  : string
The remote host.
$passive  : bool
Whether passive mode is enabled.
$port  : int
The control-channel port.
$root  : string
The remote base directory entered right after login (empty to stay in the default directory).
$secure  : bool
Whether the transport is secured (FTPS).
$security  : string
The transport security mode.
$timeout  : int
The connection timeout, in seconds.
$transferMode  : string
The default transfer mode applied to file operations.

Methods

__construct()  : mixed
Creates a new FTP client.
__destruct()  : mixed
Closes the connection on destruction.
connect()  : static
Opens the connection and authenticates, retrying transient failures with exponential backoff. A no-op when already connected.
disconnect()  : static
Closes the connection if it is open.
getCredentials()  : FtpCredentials
Returns the configured login credentials.
getHost()  : string
Returns the configured remote host.
getMaxRetries()  : int
Returns the configured maximum number of connection attempts.
getPort()  : int
Returns the configured control-channel port.
getRoot()  : string
Returns the configured remote root directory.
getTimeout()  : int
Returns the configured connection timeout.
getTransferMode()  : string
Returns the default transfer mode applied to file operations.
isConnected()  : bool
Indicates whether a connection is currently open.
isPassive()  : bool
Indicates whether passive mode is enabled.
isSecure()  : bool
Indicates whether the transport is secured (FTPS).
waitBeforeRetry()  : void
Pauses between two connection attempts. Isolated so tests can override it without changing the retry logic.
applyRoot()  : void
Changes into the configured root directory, if any, right after connecting.
ensureConnected()  : void
Asserts that the client holds an open connection.
establish()  : void
Performs a single connect + authenticate sequence.
initializeConfig()  : void
Reads the configuration array into the connection state.

Properties

$maxRetries

The maximum number of attempts for a transient (connection) failure.

public int $maxRetries = 3

$connected

Whether the client is currently connected.

private bool $connected = false

$root

The remote base directory entered right after login (empty to stay in the default directory).

private string $root = ''

$security

The transport security mode.

private string $security = \oihana\ftp\enums\FtpSecurity::NONE

$transferMode

The default transfer mode applied to file operations.

private string $transferMode = \oihana\ftp\enums\FtpTransferMode::BINARY

Methods

__construct()

Creates a new FTP client.

public __construct([array<string|int, mixed>|FtpOptions $init = [] ][, FtpDriverInterface|null $driver = null ][, ContainerInterface|null $container = null ]) : mixed
Parameters
$init : array<string|int, mixed>|FtpOptions = []

Configuration keyed by Ftp constants (or an FtpOptions instance): host, port, username, password, security, passive, timeout, maxRetries, plus any logger options.

$driver : FtpDriverInterface|null = null

The transport driver. Defaults to a NativeFtpDriver.

$container : ContainerInterface|null = null

Optional PSR-11 container used to resolve a logger service.

Tags
throws
DependencyException
ContainerExceptionInterface
NotFoundException
NotFoundExceptionInterface
ReflectionException

connect()

Opens the connection and authenticates, retrying transient failures with exponential backoff. A no-op when already connected.

public connect() : static
Tags
throws
FtpConnectionException

When the connection cannot be established after every retry.

FtpAuthenticationException

When the server rejects the credentials (no retry).

Return values
static —

This instance, for chaining.

disconnect()

Closes the connection if it is open.

public disconnect() : static
Return values
static —

This instance, for chaining.

getHost()

Returns the configured remote host.

public getHost() : string
Return values
string —

The host name or IP address.

getMaxRetries()

Returns the configured maximum number of connection attempts.

public getMaxRetries() : int
Return values
int —

The retry ceiling.

getPort()

Returns the configured control-channel port.

public getPort() : int
Return values
int —

The port number.

getRoot()

Returns the configured remote root directory.

public getRoot() : string
Return values
string —

The root directory, or an empty string when none is set.

getTimeout()

Returns the configured connection timeout.

public getTimeout() : int
Return values
int —

The timeout, in seconds.

getTransferMode()

Returns the default transfer mode applied to file operations.

public getTransferMode() : string
Return values
string —

One of the FtpTransferMode constants.

isConnected()

Indicates whether a connection is currently open.

public isConnected() : bool
Return values
bool —

True when the client is connected.

isPassive()

Indicates whether passive mode is enabled.

public isPassive() : bool
Return values
bool —

True when passive mode is used.

isSecure()

Indicates whether the transport is secured (FTPS).

public isSecure() : bool
Return values
bool —

True when TLS is used.

waitBeforeRetry()

Pauses between two connection attempts. Isolated so tests can override it without changing the retry logic.

protected waitBeforeRetry(int $seconds) : void
Parameters
$seconds : int

The number of seconds to wait.

Tags
codeCoverageIgnore

applyRoot()

Changes into the configured root directory, if any, right after connecting.

private applyRoot() : void
Tags
throws
FtpConnectionException

When the root directory cannot be entered.

ensureConnected()

Asserts that the client holds an open connection.

private ensureConnected() : void

Shared by the file and directory operations.

Tags
throws
FtpTransferException

When no connection is open.

initializeConfig()

Reads the configuration array into the connection state.

private initializeConfig(array<string|int, mixed> $init) : void
Parameters
$init : array<string|int, mixed>

The configuration keyed by Ftp constants.

On this page

Search results