Obtaining files via ssh2_scp_recv in php

328 views Asked by At

For a project i need to retreive all the files on a sftp server with a total size that amounts to about ~100 mb among all the files. I developed a script thats uses the ssh2 library and fread to get all the files, but it takes arround 10 min to get all the content. I tried ssh2_scp_recv, hoping for better performance, as shown in code bellow, but the script runs rather fast, not obtaining any file, as if the mentioned function never ran.

function get_sftp_content(){
    
$url = 'someurl.com';
$port = 22;
$username = "user";
$password = "password";

// 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 = '/ftp_files';
$remoteDir = '/download';

// download all the files
$files    = scandir('ssh2.sftp://' . $sftp . $remoteDir);
//echo '<pre>' . print_r($files, true) . '</pre>' ;
if (!empty($files)) {
  foreach ($files as $file) {
      ssh2_scp_recv($connection,$remoteDir.'/'.$file, $localDir.'/'.$file);
  }
}
}


get_sftp_content();  

Any ideas on why this happens, and is there a faster alternative to get all the files, without recurring to phpseclib?

0

There are 0 answers