2

We have changed hosting companies and where ssh2 was working it no longer works. We have ssh2 enabled on the new server along with allow_url_fopen enabled.

The only difference I can see is that the old server used PHP 5.4.45 and the new server is using PHP 5.6.28.

However, I now get the following error. Could not open remote file: ssh2.sftp://Resource id #337/my/directory/file.txt

Here is an example of my code:

$remote_host = "myhostinfohere";
$remote_port = 22;
$remote_user = "myuser";
$remote_pass = "mypassword";
$remote_dir = "/my/directory/";
$remote_file = 'file.txt';

try {
    $remote_conn = ssh2_connect($remote_host, $remote_port);    
} catch (Exception $e) {
    die("could not connect to ".$remote_host);
}

try {
    ssh2_auth_password($remote_conn, $remote_user, $remote_pass);
} catch (Exception $e) {
    die("Password failed on ".$remote_host);
}

$sftp = ssh2_sftp($remote_conn);

$fetch_string = "ssh2.sftp://$sftp" . $remote_dir . $remote_file;

//If I add this it says it doesn't exists for some reason even though I can see the file if I log in remotely.
$fileExists = file_exists($fetch_string);
if (!$fileExists) { 
    die('File does not exist');
}

$stream = fopen($fetch_string, 'r');

if (!$stream) {
    die("Could not open remote file: " . $fetch_string . "\n");
}

Again, this same code works on the old server but doesn't on the new server. What am I missing? I can easily copy the file to my server with ssh2_scp_recv() which works fine so I'm not sure what is happening with the fopen() function.

SeniorDeveloper
  • 886
  • 1
  • 12
  • 22
  • can you manually authenticate using the same user/pass on the new server? does /my/directory/file.txt exist on the new server? Does it have appropriate permissions? – ivanivan Dec 15 '16 at 17:11
  • I can manually authenticate and the connection does work. When I say new server I mean where the script is at that is used to call the other server. The other server where the file exists hasn't changed at all. Just the server that hosted the above script. – SeniorDeveloper Dec 15 '16 at 17:18
  • disable firewall and see the effect (the server where SSH script is). oh you already can make SSH request successfully right?. If so then that might be the file permission issue as mentioned by @ivanivan. – BetaDev Dec 15 '16 at 17:20
  • Which server, the one that hosts the above script? Unfortunately, I cannot disable the firewall on the server that contains the file. – SeniorDeveloper Dec 15 '16 at 17:21
  • the server where your SSH script (above script is) ... – BetaDev Dec 15 '16 at 17:26
  • Having some trouble disabling it but I ran this and executed the script and didn't see any errors. `sudo tail -f /var/log/messages |grep FIREWALL` – SeniorDeveloper Dec 15 '16 at 17:43

5 Answers5

6

After hours of trying different options I finally ran across this. http://php.net/manual/en/wrappers.ssh2.php

I changed this: $fetch_string = "ssh2.sftp://$sftp" . $remote_dir . $remote_file;

To this: $fetch_string = "ssh2.sftp://user:password@mywebsiteorip.com" . $remote_dir . $remote_file;

And presto the fopen() function started working.

I'm not sure if in the newer versions if they just deprecated the ssh2_sftp() function or if it is just a bug like @billynoah mentioned because the above link wasn't even using it.

I know how much of a pain this was so hopefully it will help someone else!

SeniorDeveloper
  • 886
  • 1
  • 12
  • 22
  • that's pretty cool - i'll have to give it a whirl in the php7 / wordpress install and see if it helps. – But those new buttons though.. Dec 15 '16 at 22:44
  • 1
    this should be marked as the correct answer, as weird as the solution seems - had the same problem after switching from 5.4 to 5.6.30-something - none of the ssh2_* methods worked, the only solution was to use the full url in fopen. – Cristi Dec 05 '18 at 11:05
3

Here $fetch_string = "ssh2.sftp://$sftp" . $remote_dir . $remote_file;

use intval($sftp) instead of $sftp

Chaitenya
  • 329
  • 2
  • 10
1

Very likely related to this bug in the ssh2 extension - which I just recently encountered:

https://bugs.php.net/bug.php?id=71376

And which breaks ssh2 update functionality in Wordpress:

https://core.trac.wordpress.org/ticket/35517

1

I also faced the same problem and I struggled a lot. For me its fixed now.
This is what I did:
My PHP version: 5.6.25
Downloaded the latest php_ssh2.dll from: https://phpfashion.com/php-ssh2-dll-for-php-5-6-and-7-0 (php_ssh2.dll for PHP 5.6 x64)
Replaced the php_ssh2.dll in the following location : "C:\wamp64\bin\php\php5.6.25\ext" & restarted all my services.

Ranjit
  • 135
  • 7
0

Make sure that sftp service is enabled on the (Powershell) server

David Lefkon
  • 289
  • 4
  • 6