Accessibility Navigation

Using PHP to connect to SFTP

Posted on 13th February 2011 by Jacob Wyke

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.


Have Your Say

Have Your Say Form









Comments

  • JB on 1st September 2011 wrote:

    Great work Jacob. I have a question:

    1.I need to send files every 3 minutes to a remote server. How can automate this work in php?

    Thanks


  • Jacob Wyke on 1st September 2011 wrote:

    @JB – You don’t. Just have cron/schedual task run the PHP script every 3 mins. If you can’t do that then you could use sleep(180) at the end of a loop in your script, but it’s not ideal and cron is the best way to go.


  • Dave on 8th January 2012 wrote:

    Hi Jacob, I am having some issues with this! my code

    however, nothing apart form ‘connect ok’ is displayed in screen?

    Hope you can help!


  • Dave on 8th January 2012 wrote:

    Damn, cant post code! i have put it to pastebin and link is in the your website box!