通过 sftp php 连接到服务器

发布于 2024-10-06 18:11:12 字数 101 浏览 1 评论 0原文

我们如何通过 sftp 连接到远程服务器来验证 php 中的登录详细信息是否有效...

我正在使用 apache 服务器...我的用途是检查用户输入的登录详细信息是否正确。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦幻的心爱 2024-10-13 18:11:12

你安装了apache服务器吗?例如:xampp?

如果这样做,那么您就已经使用了 FTP 功能:

<?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.";
}
?>

在基本的 Shell FTP 连接中,您必须定义文件或文件夹的绝对路径。

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.

情绪少女 2024-10-13 18:11:12

使用纯 PHP SFTP 客户端 phpseclib 可能会让您更轻松。这是一个例子:

<?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());
?>

正如大家所推荐的那样,libssh2 的问题在于它的部署不是很广泛,它的 API 并不是最可靠的,它不可靠,支持很差,等等。

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.

伴我老 2024-10-13 18:11:12

这会有帮助吗?

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

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";
?>
耀眼的星火 2024-10-13 18:11:12

我根据多个建议形成了一个小库。我推荐

GitHub 上的 idct sftp 客户端

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

idct sftp client on GitHub

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文