0

I'm trying: -to connect to the server -to list all the files -to read the files and echo the content.

The connection work well:

$conn_id = ssh2_connect('xxx.com', 22);
$login_result = ssh2_auth_password($conn_id, 'xxxxxx', 'xxxxxxxxx');
$sftp = ssh2_sftp($conn_id);
$dir = "ssh2.sftp://$sftp/home";
$ftp_contents = scanFilesystem($dir);

Then I loop over the directory and get all the filenames, work like a charm:

foreach($ftp_contents as  $key => $currentFilename){
echo($currentFilename);
}

Then I try to get the content of the file.... And it doesn't work.

$stream = fopen("ssh2.sftp://$sftp/home/".$currentFilename, 'r');
            echo($stream);

There is the output error:

Warning: fopen(ssh2.sftp://Resource id #4/home/myfile.xml) [function.fopen]: failed to open stream: operation failed in /home/xxxxxx/public_html/xxx.php on line 38

The line 38 is the end of the foreach.

I try with:

fopen("ssh2.sftp://$sftp/home/".$currentFilename, 'r');
fopen("ssh2.sftp://$sftp/".$currentFilename, 'r');
fopen("/home/".$currentFilename, 'r');
fopen("home/".$currentFilename, 'r');

Nothing work, always the same type of error, can some one help me please? Thanks.

update: I try with : $stream = file_get_contents("ssh2.sftp://$sftp/home/$data"); does'nt work either ...

Still got the error :

Warning: file_get_contents() [function.file-get-contents]: Unable to open ssh2.sftp://Resource id #3/xxx.xml on remote host in /home/xxxxxx/public_html/xxxx.php on line 40

I don't have a clue ... can some one help me?

Peter
  • 1,719
  • 4
  • 22
  • 31
  • 1
    i gave up on php's built in ssh2 functions after to many failed attempts, and started using: http://phpseclib.sourceforge.net –  Apr 09 '14 at 20:03
  • Thanks but this is not the point, I absolutly need to it that way, so thank you, but no thanks ! ;) – Peter Apr 09 '14 at 20:26
  • Could be a permissions issue.. – neubert Apr 10 '14 at 14:15
  • 7,7,7 is set on the folder... so it seem's to dont be an issue... any one have an idea? – Peter Apr 10 '14 at 18:23
  • I still have an error, but now its Ressource id/#3 and not #4 ... I don't have a clue. – Peter Apr 10 '14 at 20:53
  • Maybe you could write a test script with phpseclib and use it's logging capability? That obviously won't fix libssh2 but the info revealed in phpseclib's logs might give you the info to fix the issue.. (libssh2 doesn't have any sort of comparable logging) – neubert Apr 15 '14 at 20:10

1 Answers1

2

I had the same problem. Resolved by converting the $sftp resource to an integer before using it as a url, which is a string. Should work like that:

$sftp = (int)$sftp;
$stream = fopen("ssh2.sftp://$sftp/home/".$currentFilename, 'r');
  • I reset my Stack Overflow password to get back into my account to upvote this & give you my thanks for your solution, as I tried a hundred things & never got past a server error before using your solution & casting the resource to an integer. Kudos! – Davis Apr 16 '21 at 06:40