PHP code example of brick / ftp

1. Go to this page and download the library: Download brick/ftp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brick / ftp example snippets


use Brick\Ftp\FtpClient;
use Brick\Ftp\FtpException;

try {
    $client = new FtpClient();

    $host    = 'ftp.example.com'; // FTP server host name
    $port    = 21;                // FTP server port
    $ssl     = false;             // whether to open a secure SSL connection
    $timeout = 90;                // timeout in seconds

    $username = 'ftp-user';
    $password = 'p@ssw0rd';

    $client->connect($host, $port, $ssl, $timeout);
    $client->login($username, $password);

    // You usually want to set passive mode (PASV) on; see below for an explanation
    $client->setPassive(true);
} catch (FtpException $e) {
    // An error occurred!
}

echo $client->getWorkingDirectory(); // e.g. /home/ftp-user

$client->setWorkingDirectory('/home/ftp-user/archive');

$files = $client->listDirectory('.');

foreach ($files as $file) {
    // $file is an FtpFileInfo object
    echo $file->name, PHP_EOL;
}

$files = $client->recursivelyListFilesInDirectory('.');

foreach ($files as $path => $file) {
    echo $path, PHP_EOL; // a/b/c.txt
    echo $file->name, PHP_EOL; // c.txt
}

$client->rename('old/path/to/file', 'new/path/to/file');

$client->delete('path/to/file');

$client->removeDirectory('path/to/directory');

$size = $client->getSize('path/to/file'); // e.g. 123456

$client->download($localFile, $remoteFile);

$client->upload($localFile, $remoteFile);

$lines = $client->sendRawCommand('FEAT');

foreach ($lines as $line) {
    echo $line, PHP_EOL;
}

$client->close();