altayalp/ftp-client

Php 的 FTP 和 SFTP 客户端

1.0.0 2016-08-04 23:23 UTC

This package is not auto-updated.

Last update: 2024-09-26 20:09:31 UTC


README

Build Status Latest Stable Version Latest Unstable Version License

Php 5.4+ 对象导向且单元测试的 FTP 和 SFTP (ssh ftp) 处理库。

安装

确保已安装或启用了 PHP FTP 扩展。

安装库的推荐方式是通过 composer

composer require altayalp/ftp-client

此命令将在当前目录安装库。

使用方法

连接并登录到服务器

// 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');

您可以通过端口调用 SftpServer 类,或通过端口和超时调用 FtpServer 类。SFTP 的默认端口为 22,FTP 的默认端口为 21,超时的默认时间为 90 秒。

// 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');

登录服务器后,可以使用相同的函数进行 FTP 和 SFTP 操作。工厂类将返回文件或目录类实例。

如果您登录 FTP 服务器时遇到问题,登录方法之后可以使用 turnPassive() 方法。SFTP 中不存在此方法。

$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
)

此方法还接受两个可选参数。$recursive 参数可以获取子目录,$ignore 参数决定不希望在列表中出现的文件扩展名。

$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
)

此方法还接受两个可选参数。$recursive 参数可以获取子目录,$ignore 参数决定不希望在列表中出现的目录名称。

$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');

从 http 服务器上传文件到服务器

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

将文件重命名为服务器上的文件

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

将文件的 chmod 改变到服务器

$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');

更改到父目录(Sftp 中不存在)

$dir->cdUp();

获取当前工作目录

$dir->pwd();

重命名目录

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

将目录的 chmod 改变到服务器

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

删除目录

目录必须为空。

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

辅助类的使用

辅助类包含一些有用的操作方法

  • Helper::formatByte: 格式化文件大小为可读格式
  • Helper::formatDate: 格式化 Unix 时间
  • Helper::getFileExtension: 获取给定的文件扩展名
  • Helper::newName: 如果本地文件存在,则重命名文件
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

测试

首先将 phpunit.xml.dist 重命名为 phpunit.xml,然后打开文件编辑 ftp 变量。之后运行 phpunit 命令。

phpunit tests/Ftp
# or
phpunit tests/Sftp

许可协议

MIT 许可协议 (MIT)。有关更多信息,请参阅 许可文件