altayalp/ftp-client
Composer 安装命令:
composer require altayalp/ftp-client
包简介
FTP and SFTP client for Php
README 文档
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 altayalp/ftp-client
This command will install the library on current dir.
Usage
Connect and Log in to Server
// 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');
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 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');
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 altayalp\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 altayalp\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.
altayalp/ftp-client 适用场景与选型建议
altayalp/ftp-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 73.16k 次下载、GitHub Stars 达 19, 最近一次更新时间为 2016 年 08 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「ssh」 「sftp」 「ftp」 「php ftp」 「ssh ftp」 「ftp client」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 altayalp/ftp-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 altayalp/ftp-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 altayalp/ftp-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A sleek PHP wrapper around rclone with Laravel-style fluent API syntax
A flexible FTP, SSL-FTP, and SSH-based SFTP client for PHP. This lib provides helpers that are easy to use to manage the remote files.
Altax is an extensible deployment tool for PHP.
A remote FTP & S3 file manager using VueJS
A simple database backup manager for Symfony2 with support for S3, Rackspace, Dropbox, FTP, SFTP.
Cisco IOS communications and configuration parsing library
统计信息
- 总下载量: 73.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 19
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-08-04