Coder Social home page Coder Social logo

smb's Introduction

SMB

Code Coverage Build Status Scrutinizer Code Quality

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

$serverFactory = new ServerFactory();
$auth = new KerberosAuth();
$server = $serverFactory->createServer('localhost', $auth);

Note that this requires a valid kerberos ticket to already be available for php

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 exists 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);

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 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.

smb's People

Contributors

icewind1991 avatar raffis avatar mmattel avatar mwehr avatar jvillafanez avatar ariselseng avatar davidprevot avatar jkroepke avatar lucashtc avatar matthiaskuehneellerhold avatar stephanweinhold avatar

Watchers

Rudi van Hierden avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.