Accessing SMB servers with PHP

NOTE: this is not supported at this timem. Please use newer code, like

Recently, we had a need to retrieve and put files onto a Windows server from a Linux box.  I looked around for a good way to access samba or Windows file servers from within PHP, and I didn’t find anything promising.  There’s Victor Verela’s smbwebclient, but it’s really more than I was looking for, as it provides a full web gateway to the SMB server.  You could probably pull out his samba class and use that standalone, but it wasn’t quite what I was looking for.

I whipped up a simple PHP library based around the smbclient command-line executable.  It isn’t very efficient for performing lots of operations, but if you only need to move a few files around, it’s very convenient to use.

Here’s an example of it in use:

Get the code here:

Join the Conversation


  1. no esta disponible la descarga de la clase por favor te pido que la vuelvas a compartir para poder.

    1. Sorry — I migrated to a new CMS yesterday, and there are some things that aren’t working. The link should be working now.

    1. I just uploaded a newer version of the library that contains a dir() function, as well as a couple of other improvements.

      I also did quick survey of other projects found a much more thorough smbclient wrapper for PHP:

      You might want to use that one instead.

  2. Is there a way to actually move files from the windows folder? I need to mave the file and not have it stay in the windows folder

  3. How to copy from local to smbclient using php? the method to copy from smb to local is $smb->get(), what is the inverse?

    1. This is probably too late for you, but just for everybody’s benefit, you can use the put() and mput() calls to put local files onto the SMB share.

Leave a comment

Your email address will not be published. Required fields are marked *