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.