PHP code example of altayalp / ftp-client

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

    

altayalp / ftp-client example snippets


// connect to ftp server
use altayalp\FtpClient\Servers\FtpServer;

$server = new FtpServer('ftp.example.com');
$server->login('user', 'password');

// or connect to ssh server
use altayalp\FtpClient\Servers\SftpServer;

$server = new SftpServer('ssh.example.com');
$server->login('user', 'password');

// connect to ftp server
use altayalp\FtpClient\Servers\FtpServer;

$server = new FtpServer('ftp.example.com', 21, 90);
$server->login('user', 'password');

// or connect to ssh server
use altayalp\FtpClient\Servers\SftpServer;

$server = new SftpServer('ssh.example.com', 22);
$server->login('user', 'password');

$server->turnPassive();

use altayalp\FtpClient\FileFactory;

$file = FileFactory::build($server);
$list = $file->ls('public_html');
print_r($list);

Array
(
    [0] => index.php
    [1] => .gitignore
    [2] => .htaccess
    [3] => composer.json
    [4] => phpunit.xml
    [5] => robots.txt
    [6] => server.php
)

$list = $file->ls('public_html' false, array('php','html'));

Array
(
    [0] => .gitignore
    [1] => .htaccess
    [2] => composer.json
    [3] => phpunit.xml
    [4] => robots.txt
)

use altayalp\FtpClient\DirectoryFactory;

$dir = DirectoryFactory::build($server);
$list = $dir->ls('public_html');
print_r($list);

Array
(
    [0] => app
    [1] => bootstrap
    [2] => css
    [3] => packages
    [4] => vendor
)

$list = $dir->ls('public_html' false, array('packages','vendor'));
print_r($list);

Array
(
    [0] => app
    [1] => bootstrap
    [2] => css
)

$file->download('public_html/remote.html', 'local.html');

$file->upload('local.zip', 'public_html/remote.zip');

$file->wget('http://www.example.com/remote.zip', 'public_html/remote.zip');

$file->rename('public_html/oldname.zip', 'public_html/newname.zip');

$file->chmod(0777, 'public_html/file.zip');

$file->rm('public_html/remote.zip');

$file->getLastMod('public_html/remote.zip');

$file->getSize('public_html/remote.zip');

$dir->mkdir('public_html/new_directory');

$dir->cd('public_html/new_directory');

$dir->cdUp();

$dir->pwd();

$dir->rename('public_html/oldname', 'public_html/newname');

$dir->chmod(0777, 'public_html/directory');

$dir->rm('public_html/directory');

Helper::formatByte($file->getSize('public_html/dashboard.zip'));
// Will output: 32.47 Mb
Helper::formatDate($file->getLastMod('public_html/dashboard.zip'));
// Will output: 14.06.2016 23:31
// or
Helper::formatDate($file->getLastMod('public_html/dashboard'), 'd.m.Y');
// Will output: 14.06.2016
Helper::getFileExtension($fileName);
// Will output: html
$file->download('public_html/demo.html', Helper::newName('demo.html'));
// if exist local file, rename file
// demo.html renamed to demo_dae4c9057b2ea5c3c9e96e8352ac28f0c7d87f7d.html