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.