Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 872
Akhil K Nambiar Avatar asked Dec 07 '10 12:12

Akhil K Nambiar


People also ask

What is SFTP PHP?

Learn how to connect to SFTP, list files, upload and download using the PHP programming language. Guides Engineering. SFTP is a standard and secure protocol through which parties can safely transfer and share data and files. In any case, engaging with an SFTP server programatically can be challenging.

Can I SSH to SFTP server?

SFTP cannot exist without SSH — SFTP uses SSH as the binding agent to transfer files securely. In other words, SSH protocol is used in the file transfer mechanism SFTP. In fact, most SSH servers include SFTP capabilities. However, not all SFTP servers support SSH commands and actions.

Can I access a SFTP server from browser?

No major web browser supports SFTP (at least not without any addin). The "third party" need to use a proper SFTP client. Some SFTP clients can register to handle sftp:// URLs. You will then be able to paste SFTP file URL to a web browser and the browser will open the SFTP client to download the file.


3 Answers

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.

like image 185
Wesley Avatar answered Sep 28 '22 09:09

Wesley


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.

like image 29
wassail Avatar answered Sep 28 '22 11:09

wassail


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";
?>
like image 42
viMaL Avatar answered Sep 28 '22 09:09

viMaL