\Omega\Filesystem\Adapter\Ftp Ftp

Ftp Adapter Class.

This class implements the FilesystemAdapterInterface, FileFactoryInterface, ListKeysAwareInterface, and SizeCalculatorInterface to provide an FTP filesystem adapter. It enables operations such as connecting to an FTP server, listing directories, retrieving file sizes, and managing file paths on the server.

This adapter supports features such as passive mode, SSL connections, and UTF-8 encoding. It allows for creating directories on the remote server if needed and handles the necessary configuration parameters for establishing the FTP connection.

Summary

Methods
Properties
Constants
__construct
read
write
rename
exists
keys
listKeys
mtime
delete
isDirectory
listDirectory
createFile
size
close
No public properties found
No public constants found
ensureDirectoryExists
createDirectory
connection
host
username
password
port
passive
create
mode
ssl
timeout
utf8
directory
fileData
No protected constants found
isDir
fetchKeys
parseRawlist
computePath
isConnected
getConnection
connect
isLinuxListing
No private properties found
No private constants found

Properties

$connection

$connection : \FTP\Connection|null

Type

Connection|null — Holds the FTP connection resource.

$host

$host : string

Type

string — The hostname of the FTP server.

$username

$username : string

Type

string — The username for FTP authentication.

$password

$password : string

Type

string — The password for FTP authentication.

$port

$port : int

Type

int — The port number for the FTP connection (default: 21).

$passive

$passive : bool

Type

bool — Indicates whether to use passive mode for the FTP connection.

$create

$create : bool

Type

bool — Indicates whether to create directories if they do not exist.

$mode

$mode : int

Type

int — The mode for file transfers (default: FTP_BINARY).

$ssl

$ssl : bool

Type

bool — Indicates whether to use SSL for the FTP connection.

$timeout

$timeout : int

Type

int — The timeout in seconds for the FTP connection (default: 90).

$utf8

$utf8 : bool

Type

bool — Indicates whether to enable UTF-8 encoding for file names.

$directory

$directory : string

Type

string — The default directory on the FTP server.

$fileData

$fileData : array

Type

array<string|int, mixed> — Stores the file metadata.

Methods

__construct()

__construct(array  $config) : mixed

Ftp constructor.

Initializes the FTP adapter with the specified configuration options.

Parameters

array $config

Configuration options for the FTP connection. Supported options include:

  • 'host': The FTP server host (default: 'localhost').
  • 'username': The FTP username (default: 'anonymous').
  • 'password': The FTP password (default: '').
  • 'port': The FTP port (default: 21).
  • 'passive': Use passive mode (default: false).
  • 'create': Create directories if not exists (default: false).
  • 'mode': File transfer mode (default: FTP_BINARY).
  • 'ssl': Use SSL (default: false).
  • 'timeout': Connection timeout in seconds (default: 90).
  • 'utf8': Enable UTF-8 encoding for file names (default: false).

Throws

\RuntimeException

if the FTP extension is not loaded.

Returns

mixed —

read()

read(string  $key) : string|bool

Reads the content of the file.

Parameters

string $key

The key or path of the file to read.

Returns

string|bool —

The content of the file or false if it cannot be read.

write()

write(string  $key, string  $content) : int|bool

Writes the given content into the file.

Parameters

string $key

The key or path of the file to write.

string $content

The content to write into the file.

Returns

int|bool —

The number of bytes written or false on failure.

rename()

rename(string  $sourceKey, string  $targetKey) : bool

Renames a file.

Parameters

string $sourceKey

The current key or path of the file.

string $targetKey

The new key or path for the file.

Returns

bool —

True on success, false on failure.

exists()

exists(string  $key) : bool

Indicates whether the file exists.

Parameters

string $key

The key or path of the file to check.

Returns

bool —

True if the file exists, false otherwise.

keys()

keys() : array

Returns an array of all keys (files and directories).

Returns

array —

The list of keys.

listKeys()

listKeys(string  $prefix = '') : array

Lists keys that begin with the given prefix.

Parameters

string $prefix

The prefix to filter the keys (optional, defaults to an empty string).

Returns

array —

An array of keys starting with the specified prefix.

mtime()

mtime(string  $key) : int|bool

Returns the last modified time.

Parameters

string $key

The key or path of the file.

Returns

int|bool —

The last modified time as a UNIX timestamp, or false on failure.

delete()

delete(string  $key) : bool

Deletes the file.

Parameters

string $key

The key or path of the file to delete.

Returns

bool —

True on success, false on failure.

isDirectory()

isDirectory(string  $key) : bool

Check if the key represents a directory.

Parameters

string $key

The key or path to check.

Returns

bool —

True if the key is a directory, false otherwise.

listDirectory()

listDirectory(string  $directory = '') : array

Lists the contents of the specified directory.

Parameters

string $directory

The directory to list. If empty, uses the default directory.

Throws

\RuntimeException

if the directory does not exist and cannot be created.

Returns

array —

An array containing 'keys' (file paths) and 'dirs' (subdirectory paths).

createFile()

createFile(string  $key, \Omega\Filesystem\Filesystem  $filesystem) : \Omega\Filesystem\File

Creates a new File instance.

Parameters

string $key

The unique key that identifies the file in the filesystem.

\Omega\Filesystem\Filesystem $filesystem

The filesystem instance that will manage the file.

Returns

\Omega\Filesystem\File —

Returns a new instance of the File class.

size()

size(string  $key) : int|false

Retrieves the size of the specified file (key).

Parameters

string $key

The file key for which the size is being retrieved.

Returns

int|false —

The size of the file in bytes, or false if it cannot be determined.

close()

close() : void

Closes the FTP connection.

Returns

void —

ensureDirectoryExists()

ensureDirectoryExists(string  $directory, bool  $create = false) : void

Ensures that the specified directory exists.

Parameters

string $directory

The directory to check.

bool $create

Whether to create the directory if it does not exist.

Throws

\RuntimeException

if the directory does not exist and cannot be created.

Returns

void —

createDirectory()

createDirectory(string  $directory) : void

Creates the specified directory on the FTP server.

Parameters

string $directory

The directory to create.

Throws

\RuntimeException

if the directory cannot be created.

Returns

void —

isDir()

isDir(string  $directory) : bool

Checks if the specified path is a directory on the FTP server.

Parameters

string $directory

The directory path to check.

Returns

bool —

True if the path is a directory, false otherwise.

fetchKeys()

fetchKeys(string  $directory = '', bool  $onlyKeys = true) : array

Fetches all keys (file and directory paths) from the specified directory.

Parameters

string $directory

The directory to fetch keys from.

bool $onlyKeys

Whether to return only file keys.

Returns

array —

An array containing 'keys' (file paths) and 'dirs' (subdirectory paths).

parseRawlist()

parseRawlist(array  $rawlist) : array

Parses the raw listing of files and directories from the FTP server.

Parameters

array $rawlist

The raw list of files and directories.

Returns

array —

An array of parsed file and directory information.

computePath()

computePath(string  $key) : string

Computes the full path for the specified key.

Parameters

string $key

The key (file name) to compute the path for.

Returns

string —

The computed full path.

isConnected()

isConnected() : bool

Checks if the adapter is connected to the FTP server.

Returns

bool —

True if connected, false otherwise.

getConnection()

getConnection() : \FTP\Connection|null

Retrieves the current FTP connection, establishing it if necessary.

Throws

\RuntimeException

if unable to establish a connection.

Returns

\FTP\Connection|null —

The current FTP connection or null if not connected.

connect()

connect() : void

Establishes a connection to the FTP server.

Throws

\RuntimeException

if unable to connect or authenticate.

Returns

void —

isLinuxListing()

isLinuxListing(array  $info) : bool

Checks if the raw listing follows the Linux format.

Parameters

array $info

The array of information from the raw listing.

Returns

bool —

True if the listing is in Linux format, false otherwise.