Search code examples
phpcurlftpsftpfsockopen

connect to a server via sftp php


how do we connect to a remote server via sftp to verify if the login details are valid in php...

i'm using apache server... my use is to check whether the login details entered by user is correct or not.


Solution

  • You might have an easier time using phpseclib, a pure PHP SFTP client. Here's an example:

    <?php
    include('Net/SFTP.php');
    
    $sftp = new Net_SFTP('www.domain.tld');
    if (!$sftp->login('username', 'password')) {
        exit('Login Failed');
    }
    
    echo $sftp->pwd() . "\r\n";
    $sftp->put('filename.ext', 'hello, world!');
    print_r($sftp->nlist());
    ?>
    

    The problem with libssh2, as everyone's recommending, is that it's not very widely deployed, it's API isn't exactly the most reliable, it's unreliable, poorly supported, etc.