PHP code example of vmvarela / smbwebclient

1. Go to this page and download the library: Download vmvarela/smbwebclient library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vmvarela / smbwebclient example snippets




mbWebClient\Config;
use SmbWebClient\SmbClient;

// Create configuration
$config = new Config(
    smbDefaultServer: 'your-smb-server',
    hideSystemShares: true,
    hideDotFiles: true,
);

// Create client with credentials
$client = new SmbClient($config, 'username', 'password');

// List available shares
$shares = $client->listShares('your-smb-server');
foreach ($shares as $share) {
    echo $share->getName() . "\n";
}

// List directory contents
$files = $client->listDirectory('your-smb-server', 'ShareName', '/path/to/folder');
foreach ($files as $file) {
    echo sprintf("%s - %s (%d bytes)\n", 
        $file->isDirectory() ? 'DIR' : 'FILE',
        $file->getName(),
        $file->getSize()
    );
}

// Download a file
$localPath = '/local/path/to/file.txt';
$client->downloadFile('your-smb-server', 'ShareName', '/remote/file.txt', $localPath);
echo file_get_contents($localPath);

// Upload a file
$client->uploadFile('your-smb-server', 'ShareName', '/remote/path', '/local/file.txt');

// Create a directory
$client->createDirectory('your-smb-server', 'ShareName', '/new/folder');
// Delete a file
$client->deleteFile('your-smb-server', 'ShareName', '/path/to/file.txt');
// Delete a directory
$client->deleteDirectory('your-smb-server', 'ShareName', '/path/to/folder');
// Rename/move a file
$client->rename('your-smb-server', 'ShareName', '/old/name.txt', '/new/name.txt');

// For anonymous/guest access, omit credentials
$client = new SmbClient($config);

// Or explicitly pass null
$client = new SmbClient($config, null, null);



otenv\Dotenv;
use SmbWebClient\Config;
use SmbWebClient\SmbClient;

// Load .env file
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

// Create config from environment
$config = Config::fromEnv();

// Use the client
$client = new SmbClient($config, $_ENV['SMB_USER'] ?? null, $_ENV['SMB_PASS'] ?? null);