Access Windows Files from Linux Server via smbclient & PHP

Accessing to Windows files from a Linux Server in PHP without mounting a Samba partition is really easy with libsmbclient-php, a PHP extension that uses Samba’s libsmbclient library to provide Samba related functions to PHP.
After installing the library, you will be able to manipulate your files and folders just like you do in Linux by using the smb:// prefix.

smbclient Installation

Make sure you have samba, samba-client installed on your server, then install smbclient.

PECL Installation

Package Installation

See for this method, this requires Remi repository on CentOS.

From sources

Take it from GitHub then compile it.
The next step requires you to grab your Windows NTLM Authentication and make sure the user you are using with PHP has the right to access the files.

libsmbclient-php example script

This quick example PHP script shows how to use libsmbclient-php.
It opens a connection to a Windows server via a username & password, read a file, then delete it.

A simpler version can be made because most standard PHP functions will work with ‘smb’ URIs. (Like file_get_contents, file_put_contents, fopen, mkdir, opendir, unlink, …)

Leave a Reply