0

I have the following simple code, to connect to a local server and retrieve an index.php file.

$username = "Admin";
$password = "Admin";
$url = "192.168.1.33";

$connection = ssh2_connect($url);
if (!ssh2_auth_password($connection, $username, $password)) throw new Exception('Unable to connect.');

if (!$sftp = ssh2_sftp($connection)) throw new Exception('Unable to create SFTP connection.');

$localDir  = 'C:/downloadTest';
$remoteDir = '/htdocs';

$files = scandir('ssh2.sftp://' . $sftp . "/".$remoteDir);
if (!empty($files)) {
  foreach ($files as $file) {
    if ($file == "index.php") {
        //echo $remoteDir."/".$file;
        //echo $localDir.'/'.$file;
        ssh2_scp_recv($connection, "$remoteDir/$file", "$localdir/$file");
    }
  }
} 

However after executing I always get an Error:

Warning: ssh2_scp_recv(): Unable to receive remote file in C:\path\to\index\index.php on line 21

On the server (Windows 10) I enabled OpenSSH Server and added the following lines to the config:

Subsystem sftp sftp-server.exe -d "C:\Server\data\"
ChrootDirectory C:\Server\data

PermitTunnel no
AllowAgentForwarding no
AllowTcpForwarding no
X11Forwarding no
AllowUsers Admin

I am able to connect through WinSCP through the SFTP Protocol, but not the SCP protocol.

Printing the file list works, so the connection is there 100%.

Local PC & Server are Windows 10.

Edit:

I am able to download files from the root folder with the following code:

<?php

$username = "Admin";
$password = "Admin";
$url = "192.168.1.33";

$remoteDir = "";
$localDir = "downloadtest\\";

$connection = ssh2_connect($url);
if (!ssh2_auth_password($connection, $username, $password)) throw new Exception('Unable to connect.');

if(!$stream = ssh2_sftp($connection)){
  die("Unable to create stream");
}

if(!$dir = opendir("ssh2.sftp://{$stream}/{$remoteDir}")){
  die("Could not open dir");
}

$files = array();
while (false !== ($file = readdir($dir)))
{
    if ($file == "." || $file == "..")
        continue;
    $files[] = $file;
}

foreach ($files as $file){
  if(!$remote = @fopen("ssh2.sftp://{$stream}/{$remoteDir}{$file}","r")){
    echo "unable to open remote file: $file\n";
    continue;
  }
  
  if(!$local = @fopen($localDir . $file, "w")){
    echo "Unable to create local file: $file\n";
    continue;
  }

  $read = 0;
  $filesize = filesize("ssh2.sftp://{$stream}/{$remoteDir}{$file}");
  while ($read < $filesize && ($buffer = fread($remote, $filesize - $read))){
    $read += strlen($buffer);
    if(fwrite($local, $buffer) === false){
      echo "Unable to write to local file: $file\n";
    }
  }
  fclose($local);
  fclose($remote);
}

?>

However this is not through scp but sftp. With sftp I can download wherever I like (root folder or sub folder), with scp it does not work at all and always gives me the error Warning: ssh2_scp_recv(): Unable to receive remote file in C:\path\to\index\index.php on line 21

I am 100% sure it is not a rights issue, since I can download files through the cmd with the following command pscp -scp Admin@192.168.1.33:/htdocs/index.php C:\downloadTest\index.php

Dokik
  • 364
  • 2
  • 9
  • Does commandline `scp`/`pscp` work? – Martin Prikryl Jan 31 '22 at 20:54
  • Just tested and they both work – Dokik Feb 01 '22 at 07:19
  • Are they using SCP? Add `-scp` to `pscp` commandline to make sure. – Martin Prikryl Feb 01 '22 at 08:57
  • Alright, so I tried `pscp 'user@server:/htdocs/index.php' 'C:\downloadTest\index.php -scp` and it gives me the error `More than one remote source not supported` . I tried fixing it, but I cannot find anything when googling. – Dokik Feb 01 '22 at 09:06
  • True, it worked with `-scp` in front – Dokik Feb 01 '22 at 09:20
  • 1
    Ok, I do not know why `ssh2_scp_recv` does not work then. Try using SFTP, like here: https://stackoverflow.com/q/17687607/850848#17688779 – `fopen("ssh2.sftp://{$stream}/{$remoteDir}{$file}", 'r')` etc. – Martin Prikryl Feb 01 '22 at 09:27
  • Alright, I will try it like that. Still thank you for trying to help I appreciate it. – Dokik Feb 01 '22 at 09:30
  • Did it work for you? – Martin Prikryl Feb 03 '22 at 09:59
  • Well, kind of. I set `$remoteDir = "";` and I was able to download files from the root folder that I set in the OpenSSH config, but any attempt to access subfolders was unsuccessful. I also tried with the absolut path since I read in some other posts that sometimes there are issues with setting root folders and you still need to use the absolute path, but that did not help either. When I have time I will remove the root folder from the config and try again. (I only tried with your suggestion, not with `ssh2_scp_recv()`) – Dokik Feb 03 '22 at 10:08
  • So show us your code for downloading files from subfolders. And an equivalent example of working download using any commandline SFTP client. – Martin Prikryl Feb 03 '22 at 12:44
  • I updates my question, I hope this is what you wanted. I am really lost as to why it won't work. – Dokik Feb 03 '22 at 15:26
  • I'm lost. To me your edit seems to say that `ssh2_scp_recv` does not work for you with files in subfolders. But we know already that `ssh2_scp_recv` does not seem to work for you *at all*. So that's not a new information. – Did you try the `fopen` approach with files in subfolders? – Martin Prikryl Feb 04 '22 at 07:01
  • The code you see in the edit is with `fopen`, which only works when I want to download directly from the root directory, not from a subfolder. So I also tried to download from the root folder with `ssh2_scp_recv` but that did not work. If I find some time today I will clean up my question and restructure to make it more readable. – Dokik Feb 04 '22 at 07:10
  • And did you try `$remoteDir = "htdocs/"`? – Martin Prikryl Feb 04 '22 at 07:16
  • Yes, I tried pretty much every way to declare the subfolder. – Dokik Feb 04 '22 at 07:26
  • Please post [mcve]. Ideally with literal strings, so we do not have to deduce the actual URLs from number of other variables. + Are you getting the *same error* with all combinations? – Martin Prikryl Feb 04 '22 at 09:31
  • Alright, as I said above if I find the time I will restructure the question so readers have a better overview. I will have to check again for the errors. – Dokik Feb 04 '22 at 09:54
  • Updated my question I hope it makes it a bit more clear for everyone. – Dokik Feb 08 '22 at 07:53

0 Answers0