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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
require_once ('smbclient.php'); $smbc = new smbclient ('//10.0.1.1/example', 'exampleuser', 'examplepassword'); if (!$smbc->get ('path/to/desired/file.txt', '/tmp/localfile.txt')) { print "Failed to retrieve file:\n"; print join ("\n", $smbc->get_last_stdout()); } else { print "Transferred file successfully."; } |
Get the code here: https://gist.github.com/jpriebe/293aec9ba37e1d5c0570b757aab9aa74
no esta disponible la descarga de la clase por favor te pido que la vuelvas a compartir para poder.
Sorry — I migrated to a new CMS yesterday, and there are some things that aren’t working. The link should be working now.
Thank you.
Is possible to list the files in a directory?
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.
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
There’s an error in your example. On line 8 it should say get_last_cmd_stdout().
And thanks for the class! 🙂
Awesome code snippet. It made my life simple.
Why yo dont publish it in GIT?
Good idea. Updated the article to put it into a gist at github.
How to copy from local to smbclient using php? the method to copy from smb to local is $smb->get(), what is the inverse?
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.