antilam / ftp-client
FTP and SFTP client for Php
Requires
- php: >=5.4.0
- ext-ftp: *
Requires (Dev)
- phpunit/phpunit: ~4.8
This package is not auto-updated.
Last update: 2024-11-07 21:50:28 UTC
README
Php 5.4+ object oriented and unit tested library for FTP and SFTP (ssh ftp) process.
Installation
Make sure the PHP FTP extension is installed or enabled.
The recommended way to install the library is through composer.
composer require antilam/ftp-client
This command will install the library on current dir.
Usage
Connect and Log in to Server
// connect to ftp server use antilam\FtpClient\Servers\FtpServer; $server = new FtpServer('ftp.example.com'); $server->login('user', 'password'); // or connect to ssh server use antilam\FtpClient\Servers\SftpServer; $server = new SftpServer('ssh.example.com'); $server->login('user', 'password');
You can call SftpServer class by port or FtpServer class by the port and timeout. The default port for SFTP is 22, for FTP is 21 and for timeout is 90 seconds.
// connect to ftp server use antilam\FtpClient\Servers\FtpServer; $server = new FtpServer('ftp.example.com', 21, 90); $server->login('user', 'password'); // or connect to ssh server use antilam\FtpClient\Servers\SftpServer; $server = new SftpServer('ssh.example.com', 22); $server->login('user', 'password');
You can use same methods for FTP and SFTP after login server. The factory classes will return file or directory class instance.
If you have a problem login to FTP server, turnPassive() method may useful after login method. It's not exist for SFTP.
$server->turnPassive();
Fetching Files
use antilam\FtpClient\FileFactory; $file = FileFactory::build($server); $list = $file->ls('public_html'); print_r($list);
Will output:
Array ( [0] => index.php [1] => .gitignore [2] => .htaccess [3] => composer.json [4] => phpunit.xml [5] => robots.txt [6] => server.php )
This method takes two more optional parameters. $recursive also fetch subdirectories. $ignore parameter determine extension of the files which you don't want to see in list.
$list = $file->ls('public_html' false, array('php','html'));
Will output:
Array ( [0] => .gitignore [1] => .htaccess [2] => composer.json [3] => phpunit.xml [4] => robots.txt )
Fetching Directories
use antilam\FtpClient\DirectoryFactory; $dir = DirectoryFactory::build($server); $list = $dir->ls('public_html'); print_r($list);
Will output:
Array ( [0] => app [1] => bootstrap [2] => css [3] => packages [4] => vendor )
This method takes two more optional parameters. $recursive also fetch subdirectories. $ignore parameter determine name of the directories which you don't want to see in list.
$list = $dir->ls('public_html' false, array('packages','vendor')); print_r($list);
Will output:
Array ( [0] => app [1] => bootstrap [2] => css )
Other File Operations
Download file from server to local disc with rename
$file->download('public_html/remote.html', 'local.html');
Upload file from local to server with rename
$file->upload('local.zip', 'public_html/remote.zip');
Upload file from http server to server
$file->wget('http://www.example.com/remote.zip', 'public_html/remote.zip');
Rename file to server
$file->rename('public_html/oldname.zip', 'public_html/newname.zip');
Change chmod file to server
$file->chmod(0777, 'public_html/file.zip');
Remove file to server
$file->rm('public_html/remote.zip');
Get last modified time to file
$file->getLastMod('public_html/remote.zip');
Get size to file
$file->getSize('public_html/remote.zip');
Other Directory Operations
Create new directory
$dir->mkdir('public_html/new_directory');
Change current working directory
$dir->cd('public_html/new_directory');
Changes to the parent directory (not exist Sftp)
$dir->cdUp();
Get current working directory
$dir->pwd();
Rename Directory
$dir->rename('public_html/oldname', 'public_html/newname');
Change chmod directory to server
$dir->chmod(0777, 'public_html/directory');
Remove Directory
The directory must be empty.
$dir->rm('public_html/directory');
Usage Of Helper Class
Helper Class contains some useful methods for actions:
- Helper::formatByte: Format file size to human readable
- Helper::formatDate: Format unix time
- Helper::getFileExtension: Get given file extension
- Helper::newName: If exist local file, rename the file
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
Test
Firstly rename phpunit.xml.dist to phpunit.xml and than open the file to edit ftp variables. After run the phpunit command.
phpunit tests/Ftp
# or
phpunit tests/Sftp
License
The MIT License (MIT). Please see License File for more information.