nicolab / php-ftp-client
PHP 的灵活 FTP 和 SSL-FTP 客户端。此库提供了一些易于使用的辅助工具来管理远程文件。
v2.0.2
2022-08-10 11:09 UTC
Requires
- php: >=5.4
- ext-ftp: *
README
PHP 的灵活 FTP 和 SSL-FTP 客户端。此库提供了一些易于使用的辅助工具来管理远程文件。
该软件包旨在保持简单和轻量。它仅是 PHP 本地 FTP API 的包装,并提供了一些有用的辅助工具。如果您想自定义某些方法,可以通过继承软件包中的3 个类之一来实现。
安装
- 使用 composer 安装软件包
composer require nicolab/php-ftp-client
- 或者使用 GIT 克隆命令
git clone git@github.com:Nicolab/php-ftp-client.git
- 或者下载库,配置您的自动加载器或将
php-ftp-client/src/FtpClient
目录下的 3 个文件包含在内。
入门指南
连接到 FTP 服务器
$ftp = new \FtpClient\FtpClient(); $ftp->connect($host); $ftp->login($login, $password);
或者
通过 SSL 连接到 FTP 服务器(在端口 990 或其他端口)
$ftp = new \FtpClient\FtpClient(); $ftp->connect($host, true, 990); $ftp->login($login, $password);
注意:连接在脚本执行结束时(当对象被销毁时)会隐式关闭。因此,除非需要显式重新连接,否则不需要调用 $ftp->close()
。
用法
上传所有文件和所有目录非常简单
// 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);
注意:FTP_ASCII 和 FTP_BINARY 是 PHP 内置的预定义常量。
获取目录大小
// 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));
结果
'directory#www' =>
array (size=10)
'permissions' => string 'drwx---r-x' (length=10)
'number' => string '3' (length=1)
'owner' => string '32385' (length=5)
'group' => string 'users' (length=5)
'size' => string '5' (length=1)
'month' => string 'Nov' (length=3)
'day' => string '24' (length=2)
'time' => string '17:25' (length=5)
'name' => string 'www' (length=3)
'type' => string 'directory' (length=9)
'link#www/index.html' =>
array (size=11)
'permissions' => string 'lrwxrwxrwx' (length=10)
'number' => string '1' (length=1)
'owner' => string '0' (length=1)
'group' => string 'users' (length=5)
'size' => string '38' (length=2)
'month' => string 'Nov' (length=3)
'day' => string '16' (length=2)
'time' => string '14:57' (length=5)
'name' => string 'index.html' (length=10)
'type' => string 'link' (length=4)
'target' => string '/var/www/shared/index.html' (length=26)
'file#www/README' =>
array (size=10)
'permissions' => string '-rw----r--' (length=10)
'number' => string '1' (length=1)
'owner' => string '32385' (length=5)
'group' => string 'users' (length=5)
'size' => string '0' (length=1)
'month' => string 'Nov' (length=3)
'day' => string '24' (length=2)
'time' => string '17:25' (length=5)
'name' => string 'README' (length=6)
'type' => string 'file' (length=4)
支持并改进了所有 FTP PHP 函数
// 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 ...
获取远程 FTP 服务器的帮助信息
var_dump($ftp->help());
结果
array (size=6)
0 => string '214-The following SITE commands are recognized' (length=46)
1 => string ' ALIAS' (length=6)
2 => string ' CHMOD' (length=6)
3 => string ' IDLE' (length=5)
4 => string ' UTIME' (length=6)
5 => string '214 Pure-FTPd - http://pureftpd.org/' (length=36)
注意:结果取决于 FTP 服务器。
扩展
创建您的自定义 FtpClient
。
// 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');
API 文档
有关更多详细信息,请参阅源代码。它已全面文档化 📘
测试
使用 "atoum" 单元测试框架进行了测试。
许可证
MIT c) 2014, Nicolas Tallefourtane。