2014-08-26 20:46:07 +04:00
|
|
|
SMB
|
|
|
|
===
|
|
|
|
|
2016-12-14 18:22:33 +03:00
|
|
|
[![Code Coverage](https://scrutinizer-ci.com/g/icewind1991/SMB/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/icewind1991/SMB/?branch=master)
|
2014-08-26 20:46:07 +04:00
|
|
|
[![Build Status](https://travis-ci.org/icewind1991/SMB.svg?branch=master)](https://travis-ci.org/icewind1991/SMB)
|
|
|
|
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/icewind1991/SMB/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/icewind1991/SMB/?branch=master)
|
|
|
|
|
|
|
|
PHP wrapper for `smbclient` and [`libsmbclient-php`](https://github.com/eduardok/libsmbclient-php)
|
|
|
|
|
|
|
|
- Reuses a single `smbclient` instance 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`](https://github.com/eduardok/libsmbclient-php)
|
|
|
|
|
|
|
|
Examples
|
|
|
|
----
|
|
|
|
|
2018-06-21 17:02:51 +03:00
|
|
|
### Connect to a share ###
|
2014-08-26 20:46:07 +04:00
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
2018-06-21 17:02:51 +03:00
|
|
|
use Icewind\SMB\ServerFactory;
|
|
|
|
use Icewind\SMB\BasicAuth;
|
2014-08-26 20:46:07 +04:00
|
|
|
|
|
|
|
require('vendor/autoload.php');
|
|
|
|
|
2018-06-21 17:02:51 +03:00
|
|
|
$serverFactory = new ServerFactory();
|
|
|
|
$auth = new BasicAuth('test', 'workgroup', 'test');
|
|
|
|
$server = $serverFactory->createServer('localhost', $auth);
|
2014-08-26 20:46:07 +04:00
|
|
|
|
|
|
|
$share = $server->getShare('test');
|
|
|
|
```
|
|
|
|
|
2018-06-21 17:02:51 +03:00
|
|
|
The server factory will automatically pick between the `smbclient` and `libsmbclient-php`
|
|
|
|
based backend depending on what is available.
|
|
|
|
|
|
|
|
### Using anonymous authentication ###
|
2014-08-26 20:46:07 +04:00
|
|
|
|
|
|
|
```php
|
2018-06-21 17:02:51 +03:00
|
|
|
$serverFactory = new ServerFactory();
|
|
|
|
$auth = new AnonymousAuth();
|
|
|
|
$server = $serverFactory->createServer('localhost', $auth);
|
|
|
|
```
|
2014-08-26 20:46:07 +04:00
|
|
|
|
2018-06-21 17:02:51 +03:00
|
|
|
### Using kerberos authentication ###
|
|
|
|
|
|
|
|
```php
|
|
|
|
$serverFactory = new ServerFactory();
|
|
|
|
$auth = new KerberosAuth();
|
|
|
|
$server = $serverFactory->createServer('localhost', $auth);
|
|
|
|
```
|
2014-08-26 20:46:07 +04:00
|
|
|
|
2018-06-21 17:02:51 +03:00
|
|
|
Note that this requires a valid kerberos ticket to already be available for php
|
2014-08-26 20:46:07 +04:00
|
|
|
|
2018-06-21 17:02:51 +03:00
|
|
|
### Upload a file ###
|
|
|
|
|
|
|
|
```php
|
|
|
|
$share->put($fileToUpload, 'example.txt');
|
2014-08-26 20:46:07 +04:00
|
|
|
```
|
|
|
|
|
2018-06-21 17:02:51 +03:00
|
|
|
### Download a file ###
|
2014-08-26 20:46:07 +04:00
|
|
|
|
|
|
|
```php
|
2018-06-21 17:02:51 +03:00
|
|
|
$share->get('example.txt', $target);
|
|
|
|
```
|
2014-08-26 20:46:07 +04:00
|
|
|
|
2018-06-21 17:02:51 +03:00
|
|
|
### List shares on the remote server ###
|
2014-08-26 20:46:07 +04:00
|
|
|
|
2018-06-21 17:02:51 +03:00
|
|
|
```php
|
2014-08-26 20:46:07 +04:00
|
|
|
$shares = $server->listShares();
|
|
|
|
|
|
|
|
foreach ($shares as $share) {
|
|
|
|
echo $share->getName() . "\n";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### List the content of a folder ###
|
|
|
|
|
|
|
|
```php
|
|
|
|
$content = $share->dir('test');
|
|
|
|
|
|
|
|
foreach ($content as $info) {
|
2016-03-17 15:54:26 +03:00
|
|
|
echo $info->getName() . "\n";
|
2014-08-26 20:46:07 +04:00
|
|
|
echo "\tsize :" . $info->getSize() . "\n";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Using read streams
|
|
|
|
|
|
|
|
```php
|
|
|
|
$fh = $share->read('test.txt');
|
|
|
|
echo fread($fh, 4086);
|
|
|
|
fclose($fh);
|
|
|
|
```
|
|
|
|
|
|
|
|
### Using write streams
|
|
|
|
|
|
|
|
```php
|
|
|
|
$fh = $share->write('test.txt');
|
|
|
|
fwrite($fh, 'bar');
|
|
|
|
fclose($fh);
|
|
|
|
```
|
|
|
|
|
2016-12-14 18:22:33 +03:00
|
|
|
### Using notify
|
|
|
|
|
|
|
|
```php
|
|
|
|
$share->notify('')->listen(function (\Icewind\SMB\Change $change) {
|
|
|
|
echo $change->getCode() . ': ' . $change->getPath() . "\n";
|
|
|
|
});
|
|
|
|
```
|
2018-06-21 17:02:51 +03:00
|
|
|
|
|
|
|
## Testing SMB
|
|
|
|
|
|
|
|
Use the following steps to check if the library can connect to your SMB share.
|
|
|
|
|
|
|
|
1. Clone this repository or download the source as [zip](https://github.com/icewind1991/SMB/archive/master.zip)
|
|
|
|
2. Make sure [composer](https://getcomposer.org/) is installed
|
|
|
|
3. Run `composer install` in the root of the repository
|
|
|
|
4. Edit `example.php` with the relevant settings for your share.
|
|
|
|
5. Run `php example.php`
|
|
|
|
|
|
|
|
If everything works correctly then the contents of the share should be outputted.
|