2019-02-25 06:37
浏览 187


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:


ini_set('display_errors', '1');

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

foreach ($files as $file){
    $fileExists = file_exists("ssh2.sftp://". intval($sftp) . $config['folder'] . $file);
            $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);
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dougui2254 2019-02-25 06:56

    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.

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

    $session = null; unset($session); // close connection
    打赏 评论

相关推荐 更多相似问题