-2

I want to download file from SFTP so i created page which looks like it.

 <?php
     $username = "XYZ";
     $password = "ABC";
     $url ='FTP.abc.COM';
     // Make our connection
     $connection = ssh2_connect($url);
     // Authenticate
     if (!ssh2_auth_password($connection, $username, $password)) throw new Exception('Unable to connect.');
    // Create our SFTP resource
    if (!$sftp = ssh2_sftp($connection)) throw new Exception('Unable to create SFTP connection.');

   $localDir  = '/path/to/your/local/dir';
   $remoteDir = '/path/to/your/remote/dir';

   // download all the files
   $files    = scandir('ssh2.sftp://' . $sftp . $remoteDir);
   if (!empty($files))
    {
     foreach ($files as $file) {
        if ($file != '.' && $file != '..')
        {
           ssh2_scp_recv($connection, "$remoteDir/$file", "$localDir/$file");
        }
    }
}
?>

When i will call this page from browser. It will show error look like it.

Fatal error: Call to undefined function ssh2_connect() in page.php on line 6

if this method is correct then what should i edit in this page? and if there is another method then suggest me that method. Thanks in advance.

Bhavesh Patel
  • 21
  • 1
  • 2

2 Answers2

1

You'll probably have better success with phpseclib, a pure PHP SFTP implementation. eg.

<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}

// outputs the contents of filename.remote to the screen
echo $sftp->get('filename.remote');
// copies filename.remote to filename.local from the SFTP server
$sftp->get('filename.remote', 'filename.local');
?>

It has a number of advantages over libssh2:

http://phpseclib.sourceforge.net/ssh/compare.html

loud9
  • 11
  • 1
  • I have added that library in my project folder. but i am getting this error. Notice: Error reading from socket in C:\xampp\htdocs\tms\admin\Net\SSH2.php on line 2729 Notice: Connection closed by server in C:\xampp\htdocs\tms\admin\Net\SSH2.php on line 1417 Login Failed – Bhavesh Patel Apr 02 '15 at 07:50
0

SSH2 functions are not standard functions in PHP. You have to install them first. See http://php.net/manual/en/ref.ssh2.php for more details.

David Kmenta
  • 820
  • 6
  • 15
  • 2
    Also note that this PHP extension is a wrapper around the libssh2 library, and assumes this library is installed on your server. – KeithL Mar 26 '15 at 12:37