Andy Scherzinger c1555fc33e chore: Add SPDX header | vor 5 Monaten | |
---|---|---|
.. | ||
src | c1555fc33e chore: Add SPDX header | vor 5 Monaten |
.gitignore | c1555fc33e chore: Add SPDX header | vor 5 Monaten |
.php_cs.dist | c1555fc33e chore: Add SPDX header | vor 5 Monaten |
LICENSE.txt | c1555fc33e chore: Add SPDX header | vor 5 Monaten |
README.md | c1555fc33e chore: Add SPDX header | vor 5 Monaten |
composer.json | 66781e74ad update icewind/smb to 3.4.0 | vor 3 Jahren |
composer.json.license | c1555fc33e chore: Add SPDX header | vor 5 Monaten |
PHP wrapper for smbclient
and libsmbclient-php
smbclient
instance for multiple requestslibsmbclient-php
<?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.
$serverFactory = new ServerFactory();
$auth = new AnonymousAuth();
$server = $serverFactory->createServer('localhost', $auth);
There are two ways of using kerberos to authenticate against the smb server:
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);
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 passord.
The setup for such a system is fairly involved and requires roughly the following this
The client authenticates using a ticket with forwarding enabled
$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);
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);
$share->notify('')->listen(function (\Icewind\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);
Note, setting the protocol version is not supported with php-smbclient version 1.0.1 or lower.
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
.
Use the following steps to check if the library can connect to your SMB share.
composer install
in the root of the repositoryexample.php
with the relevant settings for your share.php example.php
If everything works correctly then the contents of the share should be outputted.