icewind/smb
Composer 安装命令:
composer require icewind/smb
包简介
php wrapper for smbclient and libsmbclient-php
README 文档
README
SMB
PHP wrapper for smbclient and
libsmbclient-php
- Reuses a single
smbclientinstance for multiple requests - Doesn't leak the password to the process list
- Simple 1-on-1 mapping of SMB commands
- A stream-based api to remove the need for temporary files
- Support for using libsmbclient directly trough
libsmbclient-php
Examples
Connect to a share
<?php
use Icewind\SMB\ServerFactory;
use Icewind\SMB\BasicAuth;
require('vendor/autoload.php');
$serverFactory = new ServerFactory();
$auth = new BasicAuth('user', 'workgroup', 'password');
$server = $serverFactory->createServer('localhost', $auth);
$share = $server->getShare('test');
The server factory will automatically pick between the smbclient and
libsmbclient-php based backend depending on what is available.
Using anonymous authentication
$serverFactory = new ServerFactory();
$auth = new AnonymousAuth();
$server = $serverFactory->createServer('localhost', $auth);
Using kerberos authentication
There are two ways of using kerberos to authenticate against the smb server:
- Using a ticket from the php server
- Re-using a ticket send by the client
Using a server ticket
Using a server ticket allows the web server to authenticate against the smb server using an existing machine account.
The ticket needs to be available in the environment of the php process.
$serverFactory = new ServerFactory();
$auth = new KerberosAuth();
$server = $serverFactory->createServer('localhost', $auth);
Re-using a client ticket
By re-using a client ticket you can create a single sign-on setup where the user authenticates against the web service using kerberos. And the web server can forward that ticket to the smb server, allowing it to act on the behalf of the user without requiring the user to enter his password.
The setup for such a system is fairly involved and requires roughly the following this
- The web server is authenticated against kerberos with a machine account
- Delegation is enabled for the web server's machine account
- The web server is setup to perform kerberos authentication and save the ticket in it's environment
- Php has the krb5 extension installed
- The client authenticates using a ticket with forwarding enabled
$serverFactory = new ServerFactory();
$auth = new KerberosAuth();
$auth->setTicket(KerberosTicket::fromEnv());
$server = $serverFactory->createServer('localhost', $auth);
Upload a file
$share->put($fileToUpload, 'example.txt');
Download a file
$share->get('example.txt', $target);
List shares on the remote server
$shares = $server->listShares();
foreach ($shares as $share) {
echo $share->getName() . "\n";
}
List the content of a folder
$content = $share->dir('test');
foreach ($content as $info) {
echo $info->getName() . "\n";
echo "\tsize :" . $info->getSize() . "\n";
}
Using read streams
$fh = $share->read('test.txt');
echo fread($fh, 4086);
fclose($fh);
Using write streams
$fh = $share->write('test.txt');
fwrite($fh, 'bar');
fclose($fh);
Note: write() will truncate your file to 0bytes. You may open a writeable stream with append() which will point the cursor to the end of the file or create it if it does not exist yet. (append() is only compatible with libsmbclient-php)
$fh = $share->append('test.txt');
fwrite($fh, 'bar');
fclose($fh);
Using notify
$share->notify('')->listen(function (\Icewind\SMB\Change $change) {
echo $change->getCode() . ': ' . $change->getPath() . "\n";
});
Changing network timeouts
$options = new Options();
$options->setTimeout(5);
$serverFactory = new ServerFactory($options);
Setting protocol version
$options = new Options();
$options->setMinProtocol(IOptions::PROTOCOL_SMB2);
$options->setMaxProtocol(IOptions::PROTOCOL_SMB3);
$serverFactory = new ServerFactory($options);
Note, setting the protocol version is not supported with php-smbclient version 1.0.1 or lower.
Customizing system integration
The smbclient backend needs to get various information about the system it's
running on to function such as the paths of various binaries or the system
timezone. While the default logic for getting this information should work on
most systems, it is possible to customize this behaviour.
In order to customize the integration you provide a custom implementation of
ITimezoneProvider and/or ISystem and pass them as arguments to the
ServerFactory.
Testing SMB
Use the following steps to check if the library can connect to your SMB share.
- Clone this repository or download the source as zip
- Make sure composer is installed
- Run
composer installin the root of the repository - Edit
example.phpwith the relevant settings for your share. - Run
php example.php
If everything works correctly then the contents of the share should be outputted.
icewind/smb 适用场景与选型建议
icewind/smb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.13M 次下载、GitHub Stars 达 0, 最近一次更新时间为 2014 年 07 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 icewind/smb 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 icewind/smb 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.13M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 15
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-07-26