2016-12-15 40 views
0

我们更改了托管公司,并且ssh2在其中工作的位置不再有效。我们在新服务器上启用了ssh2,并启用了allow_url_fopenSSH2无法在新服务器上工作

我能看到的唯一区别是旧服务器使用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()函数发生了什么。

+0

您可以使用相同的用户/通过新服务器手动进行身份验证吗?新服务器上是否存在/my/directory/file.txt?它是否有适当的权限? – ivanivan

+0

我可以手动进行身份验证,并且连接可以正常工作。当我说新服务器时,我指的是脚本所在的位置,用于调用其他服务器。文件存在的其他服务器根本没有改变。只是托管上述脚本的服务器。 – MagentoMan

+0

禁用防火墙并查看效果(SSH脚本所在的服务器)。哦,你已经可以成功地发出SSH请求了吗?如果是这样,那么可能是@ivanivan提到的文件权限问题。 – webDev

回答

1

很可能与这个bug在SSH2扩展 - 这是我刚刚碰到:

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

并打破了WordPress的SSH2更新功能:

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

+0

非常有趣。谢谢!直到现在我还没有看到。让我做一些进一步的挖掘。 – MagentoMan

+0

我刚刚证实我可以使用'ssh2_scp_recv()'将文件复制到我的服务器,尽管我更喜欢使用'fopen()'。有任何想法吗? – MagentoMan

3

小时后尝试不同的选择我终于碰到了这个问题。 http://php.net/manual/en/wrappers.ssh2.php

我改变了这个: $fetch_string = "ssh2.sftp://$sftp" . $remote_dir . $remote_file;

要这样: $fetch_string = "ssh2.sftp://user:[email protected]" . $remote_dir . $remote_file;

和急的fopen()功能开始工作。

我不确定是否在新版本中,如果他们只是弃用ssh2_sftp()函数,或者它只是像@ billynoah提到的错误,因为上面的链接甚至没有使用它。

我知道这是多么的痛苦,希望它会帮助别人! “:// $ SFTP ssh2.sftp”

+0

这很酷 - 我不得不在php7/wordpress安装中给它一个旋风,看看它是否有帮助。 – billynoah

0

请确保SFTP服务(PowerShell的)服务器

3

这里$ fetch_string =上启用。 $ remote_dir。 $ remote_file;

使用INTVAL($ SFTP),而不是$ SFTP

+1

该解决方案适用于我。 感谢https://bugs.php.net/bug.php?id=73597 – Chaitenya

1

我也面临同样的问题,我挣扎了很多。对我来说现在已经修好了。
这是我做过什么:
我的PHP版本:25年6月5日
从下载最新的php_ssh2.dll:https://phpfashion.com/php-ssh2-dll-for-php-5-6-and-7-0(php_ssh2.dll为PHP 5.6 X64)
在取代了php_ssh2.dll以下位置:“C:\ wamp64 \ bin \ php \ php5.6.25 \ ext” &重新启动了我的所有服务。

相关问题