PHP's ftp_ssl_connect() function is for SSL-FTP, where as what I need for a client's application is SFTP. Isn't life grand! Well, it's not really too much trouble... PHP can handle that too with functions from the ssh2 PECL extension. I'm just glad I caught it early on instead of at the 11th hour. I figured though I might as well continue my previous post about setting up this project with a brief description on installing ssh2 and testing it to ensure everything is in working order.
Installation
The ssh2 extension provides bindings to libssh2 which must be installed first on the system. My target platform is CentOS 5.3, so I was able to install libssh2 and libssh2-devel via yum (using the RPMForge repository).yum install libssh2 libssh2-develThe ssh2 extension is available from the pecl.php.net website. There is a minor bug in the current version (0.11.0) which prevents it from compiling against PHP 5.3 so I needed to apply this patch.
wget http://remi.fedorapeople.org/ssh2-php53.patchI compiled and installed the extension after the code was patched.
tar zxf ssh2-0.11.0.tgz
cd ssh2-0.11.0
patch < ../ssh2-php53.patch
phpizeAdding module=ssh2.so to php.ini and restarting Apache completed the installation. I was then ready to move on and test it to make sure it worked as it should.
./configure --with-ssh2
make
cp modules/ssh2.so /usr/local/php/lib/php/extensions/
Testing the Extension
Using the extension to upload and retrieve files programmatically over an SFTP connection is relatively simple. First, a secure shell connection is established with the SFTP server using ssh2_connect(), and then a login is authenticated with ssh2_auth_password(). Both functions return false if they fail:$sess = @ssh2_connect(SFTP_SERVER, SFTP_PORT);Once a session has been established, the ssh2_sftp() function is used to retrieve an SFTP resource.
if ($sess === false) {
echo "Connection failed.";
exit(-1);
}
$result = @ssh2_auth_password($sess, SFTP_USERNAME,
SFTP_PASSWORD);
if ($result === false) {
echo "Unable to authenticate.";
exit(-1);
}
$sftp = @ssh2_sftp($sess);From that point on, the SFTP resource is used with the ssh2.sftp filestream to read and write files.
if ($sftp === false) {
echo "Unable to initialize SFTP subsystem.";
exit(-1);
}
file_put_contents("ssh2.sftp://" . $sftp . "/tmp/test.txt",In writing my short automated test case, I dumped some bytes from /dev/urandom to generate a test file, wrote the data to the SFTP server, read it back, and compared the results to make sure they matched after the round-trip.
$data);
// generate random data for test file
$data = file_get_contents("/dev/urandom", FILE_BINARY, null,
0, 512);
$data = substr(convert_uuencode($data), 0, 512);
// write data to file on SFTP server
file_put_contents("ssh2.sftp://" . $sftp . "/tmp/test.txt",
$data);
// retrieve file from SFTP server
$retrieved = file_get_contents("ssh2.sftp://" . $sftp .
"/tmp/test.txt");
// compare original data with retrieve data
echo ($data == $retrieved) ? "Success!" : "Corruption!";
Very cool Tim! Nice, thorough explanation. Is there a function to read the contents of the directory on the remote server?
ReplyDeleteSure you can list the directory contents. You can use any file functions that work with wrappers.
ReplyDelete$files = scandir("ssh2.sftp://" . $sftp . "/home/tboronczyk");
print_r($files);
The PECL ssh2 extension is crap. If you want SFTP support, try phpseclib:
ReplyDeletehttp://phpseclib.sourceforge.net/
Perhaps, Anonymous... but generally a user-land implemented solution is going to execute slower than an extension where the bulk of its code is compiled C. Plus, the ssh2 extension gives a handle that can be used with wrappers, giving a more consistent API. I think I'll stick with the ssh2 extension for now, but thanks for the heads up!
ReplyDeleteI agree with anonymous. PECL's solution may be faster but that extra speed isn't going to amount to much if you can't even install it.
ReplyDeleteAnd as for stream wrappers... you do realize you can write your own?:
http://php.net/stream.streamwrapper.example-1
Personally, I think stream wrappers are overrated. Sure, you get a more consistent API, but you also get a more limited one. And, honestly, I don't think it's even as consistent as you're alleging. Check out the first (and only) example on this page:
http://php.net/wrappers.ssh2
They call ssh2_connect() and ssh2_auth_pubkey_file() and then pass the active handle to the stream. And yet that's consistent? Show me another stream wrapper that does that.
Who said I couldn't install the Pecl extension? ssh2 installed just fine.
ReplyDeleteI meant "you" in the generic sense. If someone (not necessarily you) can't install the Pecl extension then the speed boost one might gain from using it becomes moot.
ReplyDeleteHi,
ReplyDeleteI am not get working ssh2_scp_recv function. Here is my program:
I am getting "Unable to get remote file" error. I have tried with "sftp://some_remote_server.com" and "sftp.some_remote_server.com" as $RemoteServer value, but no luck.
For your information I am sure, I have enough read permissions at remote server and write permissions in local server.
Can't we copy a remote file into our server without $sftp = @ssh2_sftp($sess) like function?
Please let me know error in my code and how to fix it? Thanks in advance..
Regards,
N Naresh Kumar
It looks like you're forgetting the ssh. before sftp. The destination needs to be ssh2.sftp://some_remote_server.com
ReplyDeletenewbie sorry:
ReplyDeletewhat You mean when You write:
patch < ../ssh2-php53.patch ?
also:
what You mean when You write:
phpize ??
./configure --with-ssh2
can You explaine better ?
Thanks
Im a newbie.
ReplyDeleteHow am I going to set up this in xampp.