1

Recently, for a project, i have been trying to get a few files from a sftp webserver using ssh2 extension and php. So far i've managed to connect to the server and list all the files on a specific directory, but when i try to get them with ssh2_scp_recv i allways get "Warning: ssh2_scp_recv(): Unable to receive remote file in ..." . I've used another implementation, that worked, with fread, but the transfer speed are too slow, and i need some more speed.

In the implementation i've been working on i tried using the refered function inside a loop passing the file names / paths as:

ssh2_scp_recv($connection, "/download/file.json", "C:\Users\User\file.json");

but no luck. Does anyone know of a better way to achieve the goal or to surpass this problem?

Edit:

<?php

$url = 'server.url.com';
$port = 22;
$username = "username";
$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  = 'C:\Users\User';
$remoteDir = '/download';

// 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");
    }
  }
}

?>

This is the code i'am using, and i get this output:

Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26

Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26

Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26

Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26

Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26

Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26

Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26

Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26

As i was saying i suspect the problem may be the localdir path as i am using windows, but not sure as i tryed many alternatives and none worked.

  • What does "*no luck*" mean? Searching for the error you quote I found this, did you see that? https://stackoverflow.com/questions/47763513/can-you-use-ssh2-scp-recv-in-php-to-get-a-remote-directory – Don't Panic May 18 '21 at 08:49
  • Yes, i did but i cant manage to get even one file. With "no luck" i mean't this exactly, couldn't get even one file. I even deleted my code and used the one from the answer to that question. The only thing i edited where the variables, and it does the same, connects to the server, list all files in a specific directory, but isn't able to transfer them. – Sergio Rodrigues May 18 '21 at 09:03
  • It already crossed my mind, might this be some kind of bug related to using a destination path with the format provided by windows?? $localDir = 'C:\Users\User'; – Sergio Rodrigues May 18 '21 at 09:07
  • What does "*couldn't get even one file*" mean? Be specific - are you seeing errors? What happens, exactly? Anything in the logfiles? How are you making `$connection`? Edit your question, add details. See [how to create a minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve). – Don't Panic May 18 '21 at 09:26
  • Added more information to the question. – Sergio Rodrigues May 18 '21 at 09:46

0 Answers0