Accessibility Navigation

ImageXY

ImageXY - Mac OS X Batch Photo Resizer

Checkout ImageXY - Painless image resizing for Mac OS X.

Quickly and painlessly bulk resize images, change image formats and create web-friendly photos for your website.

Available on the Mac App Store!

Using PHP to connect to SFTP

When programming in a business environment you often have to send and receive files of data (data feeds) from other organisations or departments within your company. To handle this quickly and securely you can use the existing FTP protocol and its secure sibling SFTP. Today we will learn how easy it is to send/receive files over SFTP automatically, allowing your scripts to run completely autonomously.

The Requirements

There are a few things that need to be installed/compiled with PHP for this to work. So let’s get down to it:

OpenSSL
Download and install OpenSSL if you don’t already have it on your machine. Once installing it you can include OpenSSL into PHP by enabling the openssl.dll in windows or compiling PHP with ‘–with-openssl‘.

To check to see if OpenSSL is installed you can view all modules installed with either the ‘phpinfo()‘ function or ‘php -m‘ from the command line.

You can get further instructions on installation in the PHP manual.

SSH2 Extension
Install the SSH2 extension. This can be difficult for Windows servers as there is no easily available dll on the PHP site. At the time of writing the downloads below work for 5.3.X.

5.3.X – php_ssh2.dll VC6 (for Apache)
5.3.X – php_ssh2.dll VC9 (for IIS)

To install this dll, copy it to your extensions directory and include it in the php.ini file.

extension=php_ssh2.dll

Again, test to make sure the extension is installed using the instructions above.

The Code

Now that we have everything we need installed, we can get to the actual code!

<?php

$strServer = "myserver.com";
$strServerPort = "71";
$strServerUsername = "username";
$strServerPassword = "mysecret";

//connect to server
$resConnection = ssh2_connect($strServer, $strServerPort);

if(ssh2_auth_password($resConnection, $strServerUsername, $strServerPassword)){
	//Initialize SFTP subsystem
	$resSFTP = ssh2_sftp($resConnection);
	
	//
	//Send/Download file here
	//
}else{
	echo "Unable to authenticate on server";
}

?>

This will create a secure connection to the remote server and prepare you to send or download a file over SFTP.

Upload a File

All the usual file functions work so you can do either of the following:

$resFile = fopen("ssh2.sftp://{$resSFTP}/path/to/file.txt", 'w');
fwrite($resFile, "Yes it worked!");
fclose($resFile);
file_put_contents("ssh2.sftp://{$resSFTP}/path/to/file.txt", "Yes it worked!");

Download a File

$resFile = fopen("ssh2.sftp://{$resSFTP}/path/to/file.txt", 'r');
$strData = fread($resFile);
fclose($resFile);
$strData = file_get_contents("ssh2.sftp://{$resSFTP}/path/to/file.txt");

Conclusion

And that's all there is to it. You can now securely transfer files from server to server directly in your scripts.


About The Author

Jacob Wyke is a web developer from the UK who makes complex things simple on the internet. He develops a batch image resizer for Mac OS X called ImageXY which you should try if you ever need to resize anything for the web.

You should also follow him on twitter.