binfotech / smb
php wrapper for smbclient and libsmbclient-php
Requires
- php: >=7.2
- binfotech/streams: >=0.7.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpstan/phpstan: ^0.12.57
- phpunit/phpunit: ^8.5|^9.3.8
- psalm/phar: ^4.3
This package is auto-updated.
Last update: 2024-09-08 20:57:43 UTC
README
PHP wrapper for smbclient and libsmbclient-php
- 重用单个
smbclient实例处理多个请求 - 不会将密码泄露到进程列表
- 简单的SMB命令1对1映射
- 基于流的API以消除对临时文件的需求
- 支持直接通过
libsmbclient-php使用libsmbclient
示例
连接到共享
<?php use BInfotech\SMB\ServerFactory; use BInfotech\SMB\BasicAuth; require('vendor/autoload.php'); $serverFactory = new ServerFactory(); $auth = new BasicAuth('user', 'workgroup', 'password'); $server = $serverFactory->createServer('localhost', $auth); $share = $server->getShare('test');
服务器工厂将自动根据可用性在smbclient和libsmbclient-php后端之间进行选择。
使用匿名认证
$serverFactory = new ServerFactory(); $auth = new AnonymousAuth(); $server = $serverFactory->createServer('localhost', $auth);
使用kerberos认证
使用kerberos认证smb服务器有两种方式
- 使用PHP服务器上的票据
- 重用客户端发送的票据
使用服务器票据
使用服务器票据允许Web服务器使用现有的机器账户对smb服务器进行认证。
票据需要存在于PHP进程的环境中。
$serverFactory = new ServerFactory(); $auth = new KerberosAuth(); $server = $serverFactory->createServer('localhost', $auth);
重用客户端票据
通过重用客户端票据,您可以创建一个单一登录设置,其中用户使用kerberos对Web服务进行认证。Web服务器可以将该票据转发到smb服务器,使服务器能够在无需用户输入密码的情况下代表用户进行操作。
此类系统的设置相当复杂,大致需要以下这些
- Web服务器使用机器账户通过kerberos进行认证
- 为Web服务器的机器账户启用委托
- Apache配置为执行kerberos认证并将票据保存在其环境中
- PHP已安装krb5扩展
- 客户端使用启用了转发的票据进行认证
$serverFactory = new ServerFactory(); $auth = new KerberosApacheAuth(); $server = $serverFactory->createServer('localhost', $auth);
上传文件
$share->put($fileToUpload, 'example.txt');
下载文件
$share->get('example.txt', $target);
列出远程服务器上的共享
$shares = $server->listShares(); foreach ($shares as $share) { echo $share->getName() . "\n"; }
列出文件夹内容
$content = $share->dir('test'); foreach ($content as $info) { echo $info->getName() . "\n"; echo "\tsize :" . $info->getSize() . "\n"; }
使用读取流
$fh = $share->read('test.txt'); echo fread($fh, 4086); fclose($fh);
使用写入流
$fh = $share->write('test.txt'); fwrite($fh, 'bar'); fclose($fh);
注意:write()将文件截断为0字节。您可以使用append()打开可写流,将光标指向文件的末尾或创建它(如果它尚不存在)。 (append()仅与libsmbclient-php兼容)
$fh = $share->append('test.txt'); fwrite($fh, 'bar'); fclose($fh);
使用notify
$share->notify('')->listen(function (\BInfotech\SMB\Change $change) { echo $change->getCode() . ': ' . $change->getPath() . "\n"; });
更改网络超时
$options = new Options(); $options->setTimeout(5); $serverFactory = new ServerFactory($options);
设置协议版本
$options = new Options(); $options->setMinProtocol(IOptions::PROTOCOL_SMB2); $options->setMaxProtocol(IOptions::PROTOCOL_SMB3); $serverFactory = new ServerFactory($options);
注意,设置协议版本在php-smbclient版本1.0.1或更低版本中不受支持。
自定义系统集成
smbclient后端需要获取有关其运行系统的各种信息才能正常工作,例如各种二进制文件的路径或系统时区。虽然获取此信息的默认逻辑应该适用于大多数系统,但仍有可能自定义此行为。
为了自定义集成,您需要提供自定义的ITimezoneProvider和/或ISystem实现,并将它们作为参数传递给ServerFactory。
测试SMB
使用以下步骤检查库是否可以连接到您的SMB共享。
- 克隆此存储库或作为zip下载源代码
- 确保已安装composer
- 在仓库根目录下运行
composer install - 使用您共享的相关设置编辑
example.php - 运行
php example.php
如果一切运行正确,共享的内容应该会被输出。