connecting sftp using php
What is SFTP
When to use SFTP
Automating SFTP
What to consider when automating
SFTP download using php code
SFTP upload using php code
List all files on SFTP Server
What is SFTP
SFTP, Secure File Transfer Protocol, is more applicable these days where cyber security is at its peak. Whenever possible, one has to use SFTP as the communications are encrypted.
Most of tools that are used for FTP like filezilla allows using SFTP with minor adjustment. SFTP has to be supported by the server that is providing the FTP server and usually uses different port than that of the FTP.
SFTP is pretty much like FTP as far the connection and usage is concerned. It has just another layer of security to make it more secure while uploading and downloading the file.
Basic server address, username, password and port are still necessary to connect and interact with SFTP server.
When to use SFTP
If you are uploading and downloading file to the server and the server supports SFTP, it is advised to use it. The connection to the server from the client is through secure tunnel making eves dropping difficult for the perpetrators.
Especially, transferring files to and from the server that are highly sensitive like personal, financial, medical and other sensitive data should be sent through SFTP servers.
Most of the FTP tools out there do support SFTP as well so you don’t have to worry in using completely new tool for SFTP purposes. If the FTP client you are using doesn’t support, switch to Filezilla.
Automating SFTP
When you have one or two files that needs to be downloaded or uploaded to the sever once in a while, then using SFTP tool is perfectly fine. But, how about if you have to send a file to server nightly in secure manner. How about if you have to download data from sever every hour?
In the above case, you have to automate the process with choice of your programming language.
What to consider when automating SFTP server
There are a couple of precautions that you have to take when considering to automate SFTP file transfer
Never put the connection credentials on in the code
Automation requires you to have the connection credentials, username and password, to connect to sever. Don’t put those things in the code – never!
Rather, have config file or read those from database config table. This has two usages actually.
1. you don’t need to change those credentials on the code if they got compromised.
2. You won’t accidentally publish the code to public repositories and create a problem for your self.
Don’t forget to log events
Make sure you have a good logging setup on the code you are running the automation. The log should be able to tell you
if the connection to the server has been good or not,
Automation successfully uploaded/downloaded
Any exception that occur during the process.
The above log would allow you to take action accordingly provided you are taking a good look at the log or you have setup notification for it.
PHP SFTP, automating the process
Talking of the automation, there is a need for using programming language. I will show you how you can automate SFTP transaction using PHP. In this tutorial, I will show connecting sftp using php.
PHP has builtin handler for SFTP. I have tried to use it and it was not that effective for me. Rather I will show you the current module that is emerging for this purpose.
phpseclib
This is the library that is doing a great job with all php only solution. It comes with composer installation.
If you have composer installed on your system, go ahead and install it using
composer require phpseclib/phpseclib
And you will include the autoload.php
file on the file you are using
Preparing to communicate with SFTP server using php
login($sftp_credential['username'], $sftp_credential['password'])) {
//log error
}
The above code connects to sftp server and will log into the server using the credentials given.
The SFTP credentials are stored in sftp.ini
file that looks like
[sftp]
server=SEVER_INFORMATION_GOES_HERE
username=USERNAME_HERE
password=PASSWORD_HERE
port=PORT_HERE
After this point, the possibilities are unlimited.
Uploading file to SFTP Server
At this level the sftp_connection
is an object handling all the SFTP related tasks
$remote_file="/path/to/remote/SFTP/server/file.txt";
$content="Hello world SFTP";
$sftp_connection->put($remote_file, $content);
The example is self explanatory, it will upload file.txt
to the server. The content of the server in this case is “Hello world SFTP”
Downloading file from SFTP server
$remote_file="/path/to/remote/SFTP/server/file.txt";
$local_file="localfile.txt";
$sftp_connection->get($remote_file, $local_file);
In the above example, the file will be downloaded from the server and placed locally as localfile.txt
There are a number of other useful methods in the library like getting the files in the server and updating and deleting the files as well. Once the code connects to the server successfully, it has all the power to manipulate the files on the server.
Consuming SOAP tutorial – Using java eclipse
jQuery Select Option from Json
Passing composite object parameter to jersey in Restful Java web
J2EE Maven Eclipse Hello World Tutorial Part Two
using angular js with node js application example