Akhil K Nambiar
Akhil K Nambiar

Reputation: 3975

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.

Upvotes: 6

Views: 14047

Answers (4)

Bartosz Pachołek
Bartosz Pachołek

Reputation: 1308

I formed a small lib out of multiple suggestions. I encour

idct sftp client on GitHub

Upvotes: 0

wassail
wassail

Reputation: 46

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.

Upvotes: 3

viMaL
viMaL

Reputation: 638

will this help?

<?php
$connection = ssh2_connect('ftp.server.com', 22); 
if (ssh2_auth_password($connection, 'username', 'password')) 
    echo "Authentication success";
else 
    echo "Authentication failure";
?>

Upvotes: 2

Wesley
Wesley

Reputation: 800

Do you have an apache server installed? for example: xampp?

If you do then you have use the FTP function:

<?php
$connection = ssh2_connect('ftp.server.com', 22); //port 22 for Shell connections
ssh2_auth_password($connection, 'username', 'password');

$shell_ftp = ssh2_sftp($connection);

$connectionStream = fopen('ssh2.sftp://'.$shell_ftp.'/path/to/fileorfolder', 'r');

if(!connectionStream)
{
    echo "Could not connect to the server, please try agian";
}
else
{
    echo "Successfully logged in.";
}
?>

That the basic Shell FTP connection, you must define absolute path's to file's or folders.

Upvotes: 4

Related Questions