PHP wrapper for smbclient
  • PHP 98.3%
  • Shell 1.3%
  • Nix 0.3%
  • Makefile 0.1%
Find a file
Robin Appelman 0309c49668
All checks were successful
CI / Nix checks (push) Successful in 39s
CI / Unit tests - PHP 82 (push) Successful in 1m44s
CI / Unit tests - PHP 83 (push) Successful in 1m42s
CI / Unit tests - Samba 4.8.5 - smbclient 4.11.17 (push) Successful in 1m16s
CI / Unit tests - PHP 84 (push) Successful in 1m50s
CI / Unit tests - Samba 4.9.4 - smbclient 4.11.17 (push) Successful in 1m11s
CI / Unit tests - Samba latest - smbclient 4.11.17 (push) Successful in 1m39s
CI / Unit tests - Samba latest - smbclient 4.14.9 (push) Successful in 1m30s
CI / Unit tests - Samba 4.8.5 - smbclient 4.15.13 (push) Successful in 1m21s
CI / Unit tests - Samba 4.9.4 - smbclient 4.15.13 (push) Successful in 1m21s
CI / Unit tests - Samba 4.8.5 - smbclient 4.16.10 (push) Successful in 1m16s
CI / Unit tests - Samba 4.9.4 - smbclient 4.16.10 (push) Successful in 1m8s
CI / Unit tests - Samba latest - smbclient 4.16.10 (push) Successful in 1m19s
CI / Unit tests - Samba 4.9.4 - smbclient 4.17.7 (push) Successful in 1m9s
CI / Unit tests - Samba latest - smbclient 4.17.7 (push) Successful in 1m21s
CI / Unit tests - Samba 4.8.5 - smbclient 4.18.1 (push) Successful in 1m15s
CI / Unit tests - Samba 4.9.4 - smbclient 4.18.1 (push) Successful in 1m17s
CI / Unit tests - Samba latest - smbclient 4.18.1 (push) Successful in 1m20s
CI / Unit tests - Samba 4.8.5 - smbclient 4.19.5 (push) Successful in 1m18s
CI / Unit tests - Samba 4.9.4 - smbclient 4.19.5 (push) Successful in 1m30s
CI / Unit tests - Samba latest - smbclient 4.19.5 (push) Successful in 1m18s
CI / Unit tests - Samba 4.8.5 - smbclient 4.20.4 (push) Successful in 1m11s
CI / Unit tests - Samba 4.9.4 - smbclient 4.20.4 (push) Successful in 1m8s
CI / Unit tests - Samba latest - smbclient 4.20.4 (push) Successful in 1m14s
CI / Unit tests - Samba 4.8.5 - smbclient 4.21.4 (push) Successful in 1m15s
CI / Unit tests - Samba 4.9.4 - smbclient 4.21.4 (push) Successful in 1m18s
CI / Unit tests - Samba latest - smbclient 4.21.4 (push) Successful in 1m32s
CI / Unit tests (alpine) (push) Successful in 1m4s
CI / Unit tests - Samba latest - smbclient 4.15.13 (push) Successful in 1m22s
CI / Unit tests - Samba 4.8.5 - smbclient 4.14.9 (push) Successful in 1m10s
CI / Unit tests - Samba 4.9.4 - smbclient 4.14.9 (push) Successful in 1m4s
CI / Unit tests - Samba 4.8.5 - smbclient 4.17.7 (push) Successful in 1m6s
readme updates
2025-11-13 17:37:28 +01:00
.forgejo/workflows explicity set protocol in test 2025-10-26 02:54:58 +02:00
LICENSES chore: Add SPDX header 2024-06-02 20:28:14 +02:00
src 'config' classes shouldn't be final 2025-11-13 17:18:18 +01:00
stubs stub fixes 2025-10-25 21:47:01 +02:00
tests explicity set protocol in test 2025-10-26 02:54:58 +02:00
.editorconfig stub fixes 2025-10-25 21:47:01 +02:00
.envrc flake 2025-10-24 01:16:51 +02:00
.envrc.license reuse fixes 2025-10-25 01:41:15 +02:00
.gitignore new flake setup 2025-10-25 20:59:22 +02:00
.php-cs-fixer.dist.php new flake setup 2025-10-25 20:59:22 +02:00
codecov.yml formatting 2025-10-25 20:56:07 +02:00
composer.json update email 2025-11-13 15:18:33 +01:00
composer.json.license chore: Add SPDX header 2024-06-02 20:28:14 +02:00
composer.lock update phpunit 2025-10-25 23:29:20 +02:00
composer.lock.license reuse fixes 2025-10-25 23:36:46 +02:00
example-sso-kerberos.php chore: Add SPDX header 2024-06-02 20:28:14 +02:00
example.php chore: Add SPDX header 2024-06-02 20:28:14 +02:00
flake.lock nix based phpunit 2025-10-26 02:38:23 +02:00
flake.lock.license reuse fixes 2025-10-25 01:41:15 +02:00
flake.nix nix based phpunit 2025-10-26 02:38:23 +02:00
flake.nix.license reuse fixes 2025-10-25 01:41:15 +02:00
install_libsmbclient.sh chore: Add SPDX header 2024-06-02 20:28:14 +02:00
LICENSE.txt chore: Add SPDX header 2024-06-02 20:28:14 +02:00
Makefile chore: Add SPDX header 2024-06-02 20:28:14 +02:00
phpstan.neon phpstan fixes 2025-10-25 22:42:08 +02:00
phpstan.neon.license reuse fixes 2025-10-25 23:36:46 +02:00
psalm.xml stub fixes 2025-10-25 21:47:01 +02:00
psalm.xml.license reuse fixes 2025-10-25 23:36:46 +02:00
README.md readme updates 2025-11-13 17:37:28 +01:00

SMB

PHP wrapper for smbclient and 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

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.

  1. Clone this repository or download the source as zip
  2. Make sure composer 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.