从 PHP 内部进行 SFTP

发布于 2024-07-16 14:19:06 字数 241 浏览 3 评论 0原文

我正在构建一个 Web 应用程序,除了其他功能外,它还需要连接到 FTP 服务器来下载或上传文件。 该应用程序是用 PHP 编写的,托管在 Linux 服务器上。

我想知道是否也可以提供对 SFTP 服务器的支持,但在 Google 上快速搜索后,似乎这并不那么简单。

所以,问题是:在 PHP 中使用 SFTP 的最佳方法是什么? 是否有一个类可以同时提供对 FTP 和 SFTP 的支持,以便两者可以使用相同的功能?

I'm in the process of building an web app that will, besides other things, need to connect to a FTP server to download or upload files. The application is written in PHP and it's hosted on a Linux server.

What I was wondering is whether or not it would be possible to also provide support for SFTP servers, but after some quick searches on Google it seems that this is not all that simple.

So, the question is: What would be the best way to use SFTP from within PHP? Is there a class that could also provide support for FTP as well as SFTP so that same functions could be used for both?

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

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

发布评论

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

评论(5

甜嗑 2024-07-23 14:19:06

是的,您可以使用 cURL 来做到这一点。 要从 FTP 切换到 SFTP,您只需将协议选项从 CURLPROTO_FTP 更改为 CURLPROTO_SFTP

cURL 支持以下协议:HTTP、HTTPS、FTP、FTPS、SCP、SFTP、TELNET、LDAP、LDAPS、DICT、FILE、TFTP。

顺便提一句。 不要将 SFTP 与 FTPS 混淆。 SFTP 是 SSH 文件传输协议,而 FTPS 是基于 SSL 的 FTP。

Yes, you can do that with cURL. To switch from FTP to SFTP all you have to do is to change protocol option form CURLPROTO_FTP to CURLPROTO_SFTP.

cURL supports following protocols: HTTP, HTTPS , FTP, FTPS, SCP, SFTP, TELNET, LDAP, LDAPS, DICT, FILE, TFTP.

BTW. SFTP is not to be confused with FTPS. SFTP is SSH File Transfer Protocol, while FTPS is FTP over SSL.

白色秋天 2024-07-23 14:19:06

如果您没有安装 cURL(我的主机没有),您可以使用 phpseclib:

http://phpseclib.sourceforge.net/documentation/net.html#net_sftp

if you don't have cURL installed (my host doesn't), you can use phpseclib:

http://phpseclib.sourceforge.net/documentation/net.html#net_sftp

贱人配狗天长地久 2024-07-23 14:19:06

万一有人最终出现在这个页面上。

您还可以将LIBSSH2 的 PHP 绑定与 PHP 结合使用。 它应该正确安装在系统上。

在 Ubuntu 10.04 和 Debian Lenny 中,当然还有所有依赖项

apt-get install libssh2-php

In case someone end-up on this page.

You also may use the PHP Bindings for LIBSSH2 with PHP. It should be appropriately installed on the system.

In Ubuntu 10.04 and Debian Lenny, of course with all dependences

apt-get install libssh2-php
╰◇生如夏花灿烂 2024-07-23 14:19:06

Igor 的建议的问题在于,除其他外,它导致代码的可移植性大大降低(libssh2 没有安装在很多主机上),它有一个更直观的基于 OOP 的 API,并且 RSA 身份验证实际上很有意义(libssh2 需要您将公钥和私钥分别存储在文件系统上;必须单独提供它们的事实是愚蠢的,因为大多数私钥格式都包含公钥。

phpseclib 也更快:

http://kevin.vanzonneveld.net/techblog/article/ make_ssh_connections_with_php/#comment_3759

The problem with Igor's recommendation is that it, among other things, makes for much less portable code (libssh2 isn't installed on very many hosts), it has a far more intuitive OOP-based API and RSA authentication actually makes sense (libssh2 requires you store the public key and the private key separately on the file system; the fact that they have to be separately provided is silly since most private key formats include the public key within them).

phpseclib is also faster:

http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/#comment_3759

萌梦深 2024-07-23 14:19:06
$dataFile      = 'PASTE_FILE_NAME_HERE';
$sftpServer    = 'PASTE_SFTP_SERVER_NAME_HERE';
$sftpUsername  = 'PASTE_USERNAME_HERE';
$sftpPassword  = 'PASTE_PASSWORD_HERE';
$sftpPort      = 'PASTE_PORT_HERE';
$sftpRemoteDir = '/';

$ch = curl_init('sftp://' . $sftpServer . ':' . $sftpPort . $sftpRemoteDir . '/' . basename($dataFile));

$fh = fopen($dataFile, 'r');

if ($fh) {
    curl_setopt($ch, CURLOPT_USERPWD, $sftpUsername . ':' . $sftpPassword);
    curl_setopt($ch, CURLOPT_UPLOAD, true);
    curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
    curl_setopt($ch, CURLOPT_INFILE, $fh);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($dataFile));
    curl_setopt($ch, CURLOPT_VERBOSE, true);

    $verbose = fopen('php://temp', 'w+');
    curl_setopt($ch, CURLOPT_STDERR, $verbose);

    $response = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);

    if ($response) {
        echo "Success";
    } else {
        echo "Failure";
        rewind($verbose);
        $verboseLog = stream_get_contents($verbose);
        echo "Verbose information:\n" . $verboseLog . "\n";
    }
}
$dataFile      = 'PASTE_FILE_NAME_HERE';
$sftpServer    = 'PASTE_SFTP_SERVER_NAME_HERE';
$sftpUsername  = 'PASTE_USERNAME_HERE';
$sftpPassword  = 'PASTE_PASSWORD_HERE';
$sftpPort      = 'PASTE_PORT_HERE';
$sftpRemoteDir = '/';

$ch = curl_init('sftp://' . $sftpServer . ':' . $sftpPort . $sftpRemoteDir . '/' . basename($dataFile));

$fh = fopen($dataFile, 'r');

if ($fh) {
    curl_setopt($ch, CURLOPT_USERPWD, $sftpUsername . ':' . $sftpPassword);
    curl_setopt($ch, CURLOPT_UPLOAD, true);
    curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
    curl_setopt($ch, CURLOPT_INFILE, $fh);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($dataFile));
    curl_setopt($ch, CURLOPT_VERBOSE, true);

    $verbose = fopen('php://temp', 'w+');
    curl_setopt($ch, CURLOPT_STDERR, $verbose);

    $response = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);

    if ($response) {
        echo "Success";
    } else {
        echo "Failure";
        rewind($verbose);
        $verboseLog = stream_get_contents($verbose);
        echo "Verbose information:\n" . $verboseLog . "\n";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文