php sftp 段错误
如果下一个代码给我带来段错误,我需要做什么?
$handle = opendir("ssh2.sftp://$sftp".'/usr/bin/');
$file = readdir($handle);
closedir($handle);
其中 $sftp 是
$this->_connection = ssh2_connect($this->_server, 22);
if($this->_authType==ExportInterface::CONN_AUTH_KEY) {
ssh2_auth_pubkey_file($this->_connection,$this->_user,$this->_key,$this->_privateKey);
} else {
ssh2_auth_password($this->_connection,$this->_user,$this->_password);
}
$sftp = ssh2_sftp($this->_connection);
Connect 工作良好。段错误是当我只使用 readdir 函数时。
What i need to do if next code gives me seg fault ?
$handle = opendir("ssh2.sftp://$sftp".'/usr/bin/');
$file = readdir($handle);
closedir($handle);
where $sftp is
$this->_connection = ssh2_connect($this->_server, 22);
if($this->_authType==ExportInterface::CONN_AUTH_KEY) {
ssh2_auth_pubkey_file($this->_connection,$this->_user,$this->_key,$this->_privateKey);
} else {
ssh2_auth_password($this->_connection,$this->_user,$this->_password);
}
$sftp = ssh2_sftp($this->_connection);
Connect work well. and segfault is when only i use readdir function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
由于谷歌搜索排名很高,我正在回答这个老问题。我遇到了同样的分段错误,真正的解决方案不在这里。
事实证明,自 PHP 5.6 以来,函数 ssh2_sftp 的行为发生了变化,现在返回的资源不允许连接为字符串以形成文件系统路径。
因此,一旦您返回了
ssh2_sftp
,您必须首先将其传递给intval
以构建远程文件路径:不客气。
I'm answering this old question due to high ranking on Google search. I was experiencing the same segmentation fault and the real solution wasn't here.
It turns out that since PHP 5.6, the behavior of the function
ssh2_sftp
changed and is now returning a resource that doesn't allow to be concatenated as a string to form filesystem paths.So, once you have the return of the
ssh2_sftp
, you have first to pass it throughintval
in order to build the remote file path:You are welcome.
我遇到了同样的问题,我通过将 ssh2 pecl 包升级到版本 0.11.3 解决了它,
在出现一些问题后它对我有用。
问候
I had the same problem and I solved it upgrading the ssh2 pecl packaage to version 0.11.3
It worked for me after some problems.
Regards
不适用于OP的情况,但如果您未能在opendir创建的句柄上显式调用closedir,您也会在脚本结束时的PHP内部清理期间遇到段错误。
Not applicable to the OP's situation, but if you fail to call closedir explicitly on the handle created by opendir, you'll also get a segfault during PHP internal cleanup at script end.
分段(mentation)错误是 php 或 SSH/SFTP 扩展中的内部错误。您应该针对相关扩展提交错误。
不要忘记添加一个简短、可重现的示例。由于这可能只能通过 ssh 客户端和服务器的特定组合来重现,因此您应该首先在全新的 VM 中重现该错误并记录您所做的每一步,如下所示:
$ sudo apt-get install ssh
$ sudo apt-get install php5-cli
php ssh-segfault.php
并收到段错误。A seg(mentation) fault is an internal error in php or the SSH/SFTP extension. You should file a bug against the extension in question.
Don't forget to include a short, reproducible example. Since this may be only reproducible with your specific combination of ssh client and server, you should reproduce the bug first in a brand-new VM and record every step you make, like this:
$ sudo apt-get install ssh
$ sudo apt-get install php5-cli
php ssh-segfault.php
and receive a segfault.我也遇到了类似的问题,尽管还有一些其他因素。我将从解决方案开始:使用绝对路径,而不是相对路径。并避免使用
~
。至于我的发现。采用此脚本:
导致第二次
ssh2_auth_pubkey_file
调用中出现身份验证错误。在两次相同的调用之后遇到有点奇怪 - 但很容易分析。但是,如果我的应用程序代码的其余部分围绕它,则会导致段错误。最终我将范围缩小到包含一个文件,该文件定义了一个使用 __FILE__ 常量的函数,将错误转变为分段错误。
即使是像这样简单的文件:
我希望这可以在某些时候对其他人有所帮助......
I had a similar problem, though with some additional factors. I'll start with the solution: use absolute paths, rather than relative. And avoid the
~
.As for my findings. Take this script:
Results in an authentication error in the 2nd
ssh2_auth_pubkey_file
call. A bit strange to encounter, after two identical calls - but easy enough to analyze.But having the rest of my application code around it would result in the segfault. Eventually I narrowed it down to that that including a file, that defines a function that uses the
__FILE__
constant changes the error to turn into a segmentation fault.Even a file as simple as this:
I hope this helps somebody else at some point...
上面的答案之一建议您需要将资源转换为 int
intval($sftp)
以避免段错误。虽然我相信过去是这样,但现在似乎已经颠倒了,至少在 php 7.1.9 上,并且转换为 int 现在会导致段错误,至少在
file_put_contents
中:无需强制转换即可工作:
One of the answers above suggests you need to cast the resource to an int
intval($sftp)
to avoid the segfault.While I believe this used to be the case, this now appears to have inverted, at least on php 7.1.9 and casting to an int now causes the segfault, at least with
file_put_contents
:Without a cast it works: