SSH2 在新服务器上不工作
SSH2 Not Working On New Server
我们已经更换了托管公司,ssh2 工作的地方不再工作了。我们在新服务器上启用了 ssh2
,同时启用了 allow_url_fopen
。
我能看到的唯一区别是旧服务器使用 PHP 5.4.45
而新服务器使用 PHP 5.6.28
。
但是,我现在收到以下错误。
Could not open remote file: ssh2.sftp://Resource id #337/my/directory/file.txt
这是我的代码示例:
$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");
}
同样,同样的代码在旧服务器上有效,但在新服务器上无效。我错过了什么?我可以使用 ssh2_scp_recv()
轻松地将文件复制到我的服务器,这工作正常,所以我不确定 fopen()
函数发生了什么。
很可能与我最近遇到的 ssh2 扩展中的这个错误有关:
https://bugs.php.net/bug.php?id=71376
这会破坏 Wordpress 中的 ssh2 更新功能:
经过几个小时尝试不同的选择后,我终于 运行 解决了这个问题。 http://php.net/manual/en/wrappers.ssh2.php
我改变了这个:
$fetch_string = "ssh2.sftp://$sftp" . $remote_dir . $remote_file;
为此:
$fetch_string = "ssh2.sftp://user:password@mywebsiteorip.com" . $remote_dir . $remote_file;
然后 fopen()
函数开始工作。
我不确定在较新的版本中,他们是否只是弃用了 ssh2_sftp()
函数,或者它是否只是一个像 @billynoah 提到的错误,因为上面的 link 甚至没有使用它。
我知道这有多痛苦,所以希望它能帮助其他人!
确保在 (Powershell) 服务器上启用了 sftp 服务
此处 $fetch_string = "ssh2.sftp://$sftp" 。 $remote_dir。 $remote_file;
使用 intval($sftp) 代替 $sftp
我也遇到了同样的问题,纠结了好久。对我来说它现在修复了。
这就是我所做的:
我的 PHP 版本:5.6.25
从以下位置下载最新的 php_ssh2.dll:https://phpfashion.com/php-ssh2-dll-for-php-5-6-and-7-0(php_ssh2.dll for PHP 5.6 x64)
在以下位置替换了 php_ssh2.dll:"C:\wamp64\bin\php\php5.6.25\ext" 并重新启动了我的所有服务。
我们已经更换了托管公司,ssh2 工作的地方不再工作了。我们在新服务器上启用了 ssh2
,同时启用了 allow_url_fopen
。
我能看到的唯一区别是旧服务器使用 PHP 5.4.45
而新服务器使用 PHP 5.6.28
。
但是,我现在收到以下错误。
Could not open remote file: ssh2.sftp://Resource id #337/my/directory/file.txt
这是我的代码示例:
$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");
}
同样,同样的代码在旧服务器上有效,但在新服务器上无效。我错过了什么?我可以使用 ssh2_scp_recv()
轻松地将文件复制到我的服务器,这工作正常,所以我不确定 fopen()
函数发生了什么。
很可能与我最近遇到的 ssh2 扩展中的这个错误有关:
https://bugs.php.net/bug.php?id=71376
这会破坏 Wordpress 中的 ssh2 更新功能:
经过几个小时尝试不同的选择后,我终于 运行 解决了这个问题。 http://php.net/manual/en/wrappers.ssh2.php
我改变了这个:
$fetch_string = "ssh2.sftp://$sftp" . $remote_dir . $remote_file;
为此:
$fetch_string = "ssh2.sftp://user:password@mywebsiteorip.com" . $remote_dir . $remote_file;
然后 fopen()
函数开始工作。
我不确定在较新的版本中,他们是否只是弃用了 ssh2_sftp()
函数,或者它是否只是一个像 @billynoah 提到的错误,因为上面的 link 甚至没有使用它。
我知道这有多痛苦,所以希望它能帮助其他人!
确保在 (Powershell) 服务器上启用了 sftp 服务
此处 $fetch_string = "ssh2.sftp://$sftp" 。 $remote_dir。 $remote_file;
使用 intval($sftp) 代替 $sftp
我也遇到了同样的问题,纠结了好久。对我来说它现在修复了。
这就是我所做的:
我的 PHP 版本:5.6.25
从以下位置下载最新的 php_ssh2.dll:https://phpfashion.com/php-ssh2-dll-for-php-5-6-and-7-0(php_ssh2.dll for PHP 5.6 x64)
在以下位置替换了 php_ssh2.dll:"C:\wamp64\bin\php\php5.6.25\ext" 并重新启动了我的所有服务。