Accessing SMB servers with PHP

NOTE: this is not supported at this timem. Please use newer code, like https://github.com/icewind1991/SMB.

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: https://gist.github.com/jpriebe/293aec9ba37e1d5c0570b757aab9aa74

11 thoughts on “Accessing SMB servers with PHP

    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: https://github.com/icewind1991/SMB

      You might want to use that one instead.

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

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

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