PHP code example of nicolab / php-ftp-client

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

    

nicolab / php-ftp-client example snippets


$ftp = new \FtpClient\FtpClient();
$ftp->connect($host);
$ftp->login($login, $password);

$ftp = new \FtpClient\FtpClient();
$ftp->connect($host, true, 990);
$ftp->login($login, $password);

// upload with the BINARY mode
$ftp->putAll($source_directory, $target_directory);

// Is equal to
$ftp->putAll($source_directory, $target_directory, FTP_BINARY);

// or upload with the ASCII mode
$ftp->putAll($source_directory, $target_directory, FTP_ASCII);

// size of the current directory
$size = $ftp->dirSize();

// size of a given directory
$size = $ftp->dirSize('/path/of/directory');

// count in the current directory
$total = $ftp->countItems();
// or alias
$total = $ftp->count();

// count in a given directory
$total = $ftp->countItems('/path/of/directory');

// count only the "files" in the current directory
$total_file = $ftp->countItems('.', 'file');

// count only the "files" in a given directory
$total_file = $ftp->countItems('/path/of/directory', 'file');

// count only the "directories" in a given directory
$total_dir = $ftp->countItems('/path/of/directory', 'directory');

// count only the "symbolic links" in a given directory
$total_link = $ftp->countItems('/path/of/directory', 'link');

// scan the current directory and returns the details of each item
$items = $ftp->scanDir();

// scan the current directory (recursive) and returns the details of each item
var_dump($ftp->scanDir('.', true));

// Requests execution of a command on the FTP server
$ftp->exec($command);

// Turns passive mode on or off
$ftp->pasv(true);

// Set permissions on a file via FTP
$ftp->chmod(0777, 'file.php');

// Removes a directory
$ftp->rmdir('path/of/directory/to/remove');

// Removes a directory (recursive)
$ftp->rmdir('path/of/directory/to/remove', true);

// Creates a directory
$ftp->mkdir('path/of/directory/to/create');

// Creates a directory (recursive),
// creates automaticaly the sub directory if not exist
$ftp->mkdir('path/of/directory/to/create', true);

// and more ...

var_dump($ftp->help());

// MyFtpClient.php

/**
 * My custom FTP Client
 * @inheritDoc
 */
class MyFtpClient extends \FtpClient\FtpClient {

  public function removeByTime($path, $timestamp) {
      // your code here
  }

  public function search($regex) {
      // your code here
  }
}

// example.php
$ftp = new MyFtpClient();
$ftp->connect($host);
$ftp->login($login, $password);

// remove the old files
$ftp->removeByTime('/www/mysite.com/demo', time() - 86400);

// search PNG files
$ftp->search('/(.*)\.png$/i');

composer 

git clone [email protected]:Nicolab/php-ftp-client.git