logo

let’s make something together

Give us a call or drop by anytime, we endeavour to answer all enquiries within 24 hours on business days.

Find us

PO Box 16122 Collins Street West
Victoria 8007 Australia

Email us

info@domain.com
example@domain.com

Phone support

Phone: + (066) 0760 0260
+ (057) 0760 0560

Read and Download Files over SFTP using PHP and ssh2

  • By Vignesh Thandapani
  • September 7, 2017
  • 822 Views

In one of my project, there came a situation where I was needed to download files over SFTP. After quick google searches and going through many resources, I learned it. From my learnings, I have written this article and explained the process in simple steps so that the fellow developers can get benefited.

Here I am going to guide how to connect SFTP server using PHP ssh2 extension. I have learned it from PHP Manual. PHP manual is having many functions in ssh2 extension, here I am going to use only a few functions to connect the SFTP server, then to read the file data from remote and also download the remote file to our local machine.

Basic Requirements:

Before using ssh2 functions you must check the phpinfo to check whether the ssh2 extension installed. Here the steps to install ssh2 via terminal

sudo apt-get install libssh2-1-dev libssh2-php

After successful installation run the command

php -m |grep ssh2

you should see ssh2, if you are using apache, run the below command to restart the apache server

sudo service apache2 restart

if you are using nginx run the below command to restart the nginx server

sudo service nginx restart

and restart the php5-fpm

sudo service php5-fpm restart

now you can see the phpinfo will show the ssh2 extension enabled.

Code:

Use the below code to read the remote file using PHP ssh2:

<?php
$host = "your_sftp_host";
$port = 22;
$username = "your_sftp_user_name";
$password = "your_sftp_password";
$connection = NULL;
$remote_file_path = "/your_path/file_name.log";
try {
   $connection = ssh2_connect($host, $port);
   if(!$connection){
       throw new \Exception("Could not connect to $host on port $port");
   }
   $auth  = ssh2_auth_password($connection, $username, $password);
   if(!$auth){
       throw new \Exception("Could not authenticate with username $username and password ");  
   }
   $sftp = ssh2_sftp($connection);
   if(!$sftp){
       throw new \Exception("Could not initialize SFTP subsystem.");  
   }
   $stream = fopen("ssh2.sftp://".$sftp.$remote_file_path, 'r');
   if (! $stream) {
       throw new \Exception("Could not open file: ");
   }
   $contents = stream_get_contents($stream);
   echo "<pre>"; print_r($contents); echo "</pre>";
   @fclose($stream);
   $connection = NULL;
} catch (Exception $e) {
   echo "Error due to :".$e->getMessage();
}
?>

Use the below code to download file from SFTP using PHP ssh2:

<?php
$host = "your_sftp_host";
$port = 22;
$username = "your_sftp_user_name";
$password = "your_sftp_password";
$connection = NULL;
$remote_file_path = "/your_path/file_name.log";
$local_file_path = "./file_name.log";
try {
   $connection = ssh2_connect($host, $port);
   if(!$connection){
       throw new \Exception("Could not connect to $host on port $port");
   }
   $auth  = ssh2_auth_password($connection, $username, $password);
   if(!$auth){
       throw new \Exception("Could not authenticate with username $username and password ");  
   }
   $sftp = ssh2_sftp($connection);
   if(!$sftp){
       throw new \Exception("Could not initialize SFTP subsystem.");  
   }
   if(ssh2_scp_recv($connection, $remote_file_path, $local_file_path)) {
       echo "File Download Success";
   } else {
       echo "File Download Failed";
   }
   $connection = NULL;
} catch (Exception $e) {
   echo "Error due to :".$e->getMessage();
}
?>

Explanation:

ssh2_connect – Used to establish a connection to a remote SSH server. Once established the connection we need to then authenticate using either password or public key, here i am using plain password method.

ssh2_auth_password – Used to authenticate the SFTP with the plain password.

ssh2_sftp – Used to request the SFTP subsystem from an already connected SSH2 server.

fopen – Used to open the remote file.

stream_get_contents – Used to read all the contents inside the remote file.

ssh2_scp_recv – Used to copy a file from the remote server to the local filesystem using the SCP protocol.

Conclusion:

This a sample example for reading the content of the remote file, printing it on the browser screen, and downloading the remote file into our machine. Hope this article helped you. To keep yourself updated on new things in web development follow us. Our team at agira technologies constantly learn and adopt new things for our projects.

Vignesh Thandapani

Vignesh Thandapani

An enthusiastic Tech Lead with 6 plus years of experience in Web development arena. Owns legitimate experience in CorePHP, Laravel, Symfony, CakePHP, Wordpress, Joomla. Behalf, a young Aspiring "Travel admirer" craves to live with Nature.

Leave a Reply

Your email address will not be published. Required fields are marked *