当我尝试关闭ssh2连接时,没有回复

I wrote a php script to execute locally, on an apache server with php7.3, doing the following:

  1. access server via ssh2
  2. check if a file exists
  3. close connection
  4. printing json data object as a response.

script works without problems except for closing connection. if I add ssh2_disconnect function, no response comes back. What am I missing? Here is my code:

<?php

error_reporting(1);
ini_set('display_errors', '1');

    $config = ["server"=>"10.1.201.1","port"=>"22","user"=>"root","password"=>"root","folder"=>"/"];
    $files = ["file1.pdf","file2.pdf"];
    $result = [];
    $ftpConnect = ssh2_connect($config['server'],$config['port']);
        ssh2_auth_password($ftpConnect,$config['user'],$config['password']);
    $sftp = ssh2_sftp($ftpConnect);

foreach ($files as $file){
    $fileExists = file_exists("ssh2.sftp://". intval($sftp) . $config['folder'] . $file);
        if($fileExists){
            $result[$file]= ["status"=>"Found"];
        }else $result[$file]= ["status"=>"Not found"];
}
//ssh2_disconnect($ftpConnect); only if uncommented, script wouldn't work
header('content-type:text/json; charset=UTF-8');
echo json_encode($result);
?>
dongre6270
dongre6270 即使他们不是你不在他的局域:))。你不知道10.1.201.1是谁。
一年多之前 回复
duanmu2015
duanmu2015 确定他们是:)
一年多之前 回复
duankang5285
duankang5285 希望那些是假凭证
一年多之前 回复

1个回答

It seems to be version dependant. ssh2_disconnect is the only function you use to be available with PECL ssh2 >= 1.0 all the other ssh2 functions are available for PECL ssh2 >= 0.9.0. The following comment let us think php >= 7 is also needed.

http://php.net/manual/function.ssh2-disconnect.php#123413

Here is how he suggest to close the connexion without ssh2_disconnect available :

$session = null; unset($session); // close connection
dongzhi5587
dongzhi5587 谢谢你的时间,是的,这可能是一个bug,并且还有更安全的方法:)
一年多之前 回复
douzuo0002
douzuo0002 该功能可能是错误的。 bugs.php.net/bug.php?id=73438&edit=1
一年多之前 回复
douxian1895
douxian1895 我试图重现它但不能。 没有ssh2_disconnect一切都很好,当我取消注释时输出如下:{“file1.pdf”:{“status”:“Not found”},“file2.pdf”:{“status”:“Not found”} php:channel.c:2466:_libssh2_channel_free:断言`session'失败。 放弃(核心倾倒)
一年多之前 回复
douxu9707
douxu9707 我通过if(function_exists('ssh2_disconnect'))echo'1'测试了函数的存在性; 它成功了。
一年多之前 回复
dtp791357
dtp791357 如果是这样,我不应该收到错误吗? 像错误代码和'未定义的功能'? 因为根本没有回应。
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐