Windows files access from Linux in PHP
+
Par Remi le mercredi 9 septembre 2015, 14:07 - Développement - Lien permanent
I just discover the libsmbclient-php project, here some information.
Digging in owncloud code and dependencies, I just notice some libraries and a PHP extension:
- icewind/streams a set of generic stream wrappers
- icewind/smb a php wrapper for smbclient and libsmbclient-php
- libsmbclient-php a PHP extension wrapping the libsmbclient library.
In all case, to access Windows files from Linux, you need to use the Samba software.
As I understand the need of a pure PHP implementation (icewind/smb), I think that a wrapper above the smbclient command is terribly ugly, and really not robust.
From a first look, libsmbclient-php is interesting, but quite messy to use, as you have to use dedicated functions (smbclient_*).
For example, to read a Windows file, you need to write:
// Create new state: state = smbclient_state_new(); // Initialize the state with workgroup, username and password: smbclient_state_init($state, null, 'testuser', 'password'); // Open a file for reading: $file = smbclient_open($state, 'smb://server/testshare/file.txt', 'r'); if ($file) { // Read the file incrementally, dump contents to output: while ($data = smbclient_read($state, $file, 1000)) { echo $data; } } // Close the file handle: smbclient_close($state, $file); // Free the state smbclient_state_free($state);
Not fun ;) of course it's possible to create a streamWrapper to make its use more simple, such as the one provided by icewin/smb.
So I start to contribute to this project to improve it:
- Add 'smb' streams support (PR merged) was my main goal
- PHP 7 compatibility (PR merged) which seems mandatory
So, previous example can be written more simply:
readfile("smb://testuser:password@server/testshare/file.txt");
Some more work are still on their way:
- Rename extension to smbclient (PR pending)
- PECL release (to give more visibility to this nice extension)
In the future, it could also be interesting to switch from resource to object (e.g; SmbClient\State, SmbClient\File, SmbClient\Dir).
Of course, RPM of php-libsmbclient are available in my repository, version 0.7.0 in remi, remi-php55 and remi-php56 and version 0.8.0-dev in remi-test and remi-php70.
Test and feedback are very welcome.
Commentaires
You think that's bad? You should see what they used to use (pre-8.1):
http://www.phpclasses.org/package/4...
(of course, the code in OC was an undocumented fork, not the same as the original...)
Extension renamed to "smbclient", so php-smbclient (updated to version 0.8.0-rc1) is now in the repository (and should be soon submitted for official Fedora repository)
php-smbclient submitted for Fedora / EPEL repositories, Review #1263958