Peter
Peter

Reputation: 1919

Error while trying to get content of a file using fopen and SSH2

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?

Upvotes: 0

Views: 5370

Answers (1)

Artem Samoilyk
Artem Samoilyk

Reputation: 21

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');

Upvotes: 2

Related Questions