connect to a server via sftp php

Akhil K Nambiar picture Akhil K Nambiar · Dec 7, 2010 · Viewed 9.6k times · Source

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.

Answer

Wesley picture Wesley · Dec 7, 2010

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.