PHP code example of lazzard / php-ftp-client

1. Go to this page and download the library: Download lazzard/php-ftp-client 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/ */

    

lazzard / php-ftp-client example snippets




azzard\FtpClient\Connection\FtpSSLConnection;
use Lazzard\FtpClient\Config\FtpConfig;
use Lazzard\FtpClient\FtpClient;

try {
    if (!extension_loaded('ftp')) {
        throw new \RuntimeException("FTP extension not loaded.");
    }

    $connection = new FtpSSLConnection('host', 'username', 'password');
    $connection->open();

    $config = new FtpConfig($connection);
    $config->setPassive(true);

    $client = new FtpClient($connection);

    print_r($client->getFeatures());

    $connection->close();

} catch (Throwable $ex) {
    print_r($ex->getMessage());
}

// download a remote file
$client->download('path/to/remote/file', 'path/to/local/file');

// upload a local file to remote server
$client->upload('path/to/local/file', 'path/to/remote/file');

// download a remote file asynchronously
$client->asyncDownload('path/to/remote/file', 'path/to/local/file', function ($state) {
    // do something every second while downloading this file
}, 1, FtpWrapper::BINARY);

// upload a remote file asynchronously
$client->asyncUpload('path/to/local/file', 'path/to/remote/file', function ($state) {
    // do something 
}, 1, FtpWrapper::BINARY);

// get files names within an FTP directory
$client->listDir('path/to/directory');

// get only directories
$client->listDir('path/to/directory', FtpClient::DIR_TYPE);

// get detailed information of each file within a remote directory including 
// the file path of each file
$client->listDirDetails('path/to/directory');

// recursively
$client->listDirDetails('path/to/directory', true);

// copy a remote file/directory to another directory
$client->copy('path/to/remote/source', 'path/to/remote/directory');

// copy a local file/directory to the server
$client->copyFromLocal('path/to/local/file', 'path/to/remote/directory'); 

// copy a remote file/directory to local machine
$client->copyToLocal('path/to/remote/source', 'path/to/local/directory'); 

// get all png files within the giving directory with their details
$client->find('/.*\.png$/i', 'path/to/directory'); 

// recursively
$client->find('/.*\.png$/i', 'path/to/directory', true); 

// get file size
$client->fileSize('path/to/file');

// get directory size
$client->dirSize('path/to/directory');

// create an FTP file
$client->createFile('path/to/file');

// create a file with content
$client->createFile('path/to/file', 'Hello world!!');

// create a remote directory
// note: this method supports recursive directory creation
$client->createDir('directory');

// append the giving content to a remote file
$client->appendFile('path/to/file', $content);

// remove an FTP file
$client->removeFile('path/to/file');

// remove an FTP directory (be careful all the files within this directory will be removed)
$client->removeDir('path/to/directory');

// rename an FTP file/directory
$client->rename('path/to/file', $newName);

// move an FTP file or directory to another folder
$client->move('path/to/file', 'path/to/directory');

// get the count of all the files within a directory
$client->getCount('path/to/directory');

// recursively
$client->getCount('path/to/directory', true);

// recursively and files only
$client->getCount('path/to/directory', true, FtpClient::FILE_TYPE);

// set a permissions on the giving FTP file/directory 
$client->setPermissions('path/to/file', [
    'owner' => 'r-w', // read & write
    'group' => 'w',
    'world' => 'w-r-e'
]);

// or you can use the UNIX file permission digits 
$client->setPermissions('path/to/file', 777);

// is an ftp directory ?
$client->isDir('path/to/file/or/directory');

// is a file type ?
$client->isFile('path/to/file/or/directory');

// is an empty file/directory ?
$client->isEmpty('path/to/file/or/directory');

// is exists on the FTP server ?
$client->isExists('path/to/file/or/directory');

// is the server support the size feature ?
$client->isFeatureSupported('SIZE');

// get the last modified time of the giving file (not working with directories)
$client->lastMTime('path/to/file');

// get a content of an FTP file
$client->getFileContent('path/to/file', FtpWrapper::ASCII);

// get all supported features by the FTP server
$client->getFeatures();

// get the server system
$client->getSystem();

// send a request to allocate a space of bytes for the next transfer operation
// some FTP servers 

composer