0

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?

  • 1
    Depending what type of files (binary, text) are on the remote side and how often they change, it will be faster to *rsync* them (assumption: the files on the local side where your PHP script runs are not deleted after script has finished). – Martin May 25 '21 at 19:36
  • The files change every half hour or so, and are presented in json. Rsync sounds like a good idea. I'll search about the topic. Thx – Sergio Rodrigues May 25 '21 at 19:50
  • You do no error checking on the results of `ssh2_scp_recv` so you might want to start with that. SCP and SFTP are not going to be noticably different in terms of performance. – miken32 May 25 '21 at 21:44
  • In the code i posted no, but i already did and no errors were displayed. – Sergio Rodrigues May 25 '21 at 21:54

0 Answers0