使用sftp和ssh2时,fopen出现分段错误

7

我有一个运行在AWS上的PHP系统,其中有一个类可以使用shh2sftp上传xlsx文件到外部服务器。这段代码一直正常工作,直到最近AWS包openssh-clients-6.6.1p1-31.62openssh-server-6.6.1p1-31.62升级后,fopen期间出现segfault错误。 Fopen会在外部服务器上创建一个文件。 以下是代码:

$stream = @fopen("ssh2.sftp://$this->sftp$remote_file", 'w');

然后我使用$stream来写内容,但代码在fopen处停止了,出现了段错误。 我没有找到关于这个问题的任何信息,我认为问题是opnessh的新升级,因为php代码没有改变。 有什么想法吗?

这是一个大问题。肯定不止我们遇到过它... - BigTon
我在Debian 8机器上遇到了同样的问题(段错误)。这是在我安装更新后开始出现的: 开始日期:2016年12月14日12:53:53 命令行:apt-get upgrade 升级: apt:amd64 libapache2-mod-php5:amd64 php5-mysql:amd64 python-urllib3:amd64 php5-common:amd64 php5-curl:amd64 apt-utils:amd64 php5-readline:amd64 libapt-inst1.5:amd64 php5:amd64 libapt-pkg4.12:amd64 php5-cli:amd64 python-requests:amd64 php-pear:amd64 php5-intl:amd64 - Christian
5个回答

18

在StackOverflow上找到了答案: https://dev59.com/gWox5IYBdhLWcg3w_ZR0#40972584

自从此次PHP更新以来,您必须使用intval()将主机部分(ssh2_sftp()的结果)括起来:

$handle = opendir("ssh2.sftp://".intval($sftp)."/path/to/directory");
在我的情况下,使用了fopen而不是opendir,但解决方法是相同的。

8
即使使用 intval($sftp) 的解决方案,如果您使用 ssh2_disconnect() 关闭连接,仍可能遇到 "分段错误(segmentation fault)" 问题。正确的解决方案是在关闭 SSH2 连接之前关闭 SFTP 连接,使用 unset($sftp)$sftp = null。以下是我完全可用的远程文件读取示例:
if (!$ssh2_connection = ssh2_connect($host, $port)) {
    die("Failed to connect\n");
}
if (!ssh2_auth_password($ssh2_connection, $username, $password)) {
    die("Failed to login\n");
}
if (!$sftp_connection = ssh2_sftp($ssh2_connection)) {
    die("Failed to open SFTP session\n");
}
if (!$fh = fopen("ssh2.sftp://".intval($sftp_connection).$path, 'r')) {
    die("Failed to open file\n");
}
while (($s = fgets($fh)) !== false) {
    echo $s;
}
fclose($fh);
unset($sftp_connection);
if (!ssh2_disconnect($ssh2_connection)) {
    die("Failed to disconnect\n");
}

另外,当出现“段错误”时,您可能会看到以下内容:

php: channel.c:2570: _libssh2_channel_free: Assertion 'session' failed.

中止

我的解决方案解决了这个问题。


3
在我们的情况下,intval() 没有解决分段错误。然而,更改调用格式确实有效:
fopen("ssh2.sftp://{$username}:{$password}@{$host}:{$port}/{$absolutePath}/{$filename}", 'w');

1

我遇到了相同的错误,但我找到了这个PHP bug(请参见来自[2016-12-15 09:47 UTC]的ovoelker at wavecon dot de的帖子)

  1. 添加php5-dev以编译pecl模块
  2. 重新安装ssh2 pecl模块。
  3. 重启apache并正常工作

0
ssh2在我的情况下(php8)在关闭时引发了段错误,这是在使用$dir = dir(...)函数而没有调用$dir->close();的情况下发生的。我认为如果使用opendir()也会有相同的行为。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接