简体   繁体   中英

php ssh2 fgets();

im try to open a file on a remote host and read it, but i got a return value of 1. Maybe anyone find my bug.

public function get()

        {
            return $stream = file_get_contents("ssh2.sftp".$this->connection."/home/user/logfile.txt");
        }

$this->connection is:

public function __construct($host , $port , $user , $pass )
{
    if(!$this->connection = ssh2_connect($host, $port)) return -1;
    if(ssh2_auth_password( $this->connection, $user, $pass )) return -1;
    return 1;
}

Connection works also, can send other commands like tar ...

Might I recommend phpseclib, a pure PHP SFTP implementation ? It's easier to use and much better supported. Here's how you'd do it with that:

<?php
$sftp = new Net_SFTP($host, $port);
if (!$sftp->login($user, $pass)) {
    exit('bad login');
}
echo $sftp->get('/home/user/logfile.txt');
?>

If you have problems with phpseclib you can create log files and post them on the official support forums and get top notch support there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM