PHP 中的 SFTP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/717854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SFTP from within PHP
提问by titel
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.
我正在构建一个 Web 应用程序,除其他事项外,该应用程序还需要连接到 FTP 服务器以下载或上传文件。该应用程序是用 PHP 编写的,并托管在 Linux 服务器上。
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.
我想知道是否也可以提供对 SFTP 服务器的支持,但是在 Google 上进行了一些快速搜索后,似乎这并不那么简单。
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?
所以,问题是:在 PHP 中使用 SFTP 的最佳方式是什么?是否有一个类也可以提供对 FTP 和 SFTP 的支持,以便可以对两者使用相同的功能?
采纳答案by vartec
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_FTPto CURLPROTO_SFTP.
是的,你可以用cURL做到这一点。要从 FTP 切换到 SFTP,您只需将协议选项形式更改CURLPROTO_FTP为CURLPROTO_SFTP.
cURL supports following protocols: HTTP, HTTPS , FTP, FTPS, SCP, SFTP, TELNET, LDAP, LDAPS, DICT, FILE, TFTP.
cURL 支持以下协议: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.
顺便提一句。不要将 SFTP 与 FTPS 混淆。SFTP 是 SSH 文件传输协议,而 FTPS 是 FTP over SSL。
回答by dreamafter
if you don't have cURL installed (my host doesn't), you can use phpseclib:
如果您没有安装 cURL(我的主机没有),您可以使用 phpseclib:
http://phpseclib.sourceforge.net/documentation/net.html#net_sftp
http://phpseclib.sourceforge.net/documentation/net.html#net_sftp
回答by Igor
In case someone end-up on this page.
如果有人最终出现在此页面上。
You also may use the PHP Bindings for LIBSSH2with PHP. It should be appropriately installed on the system.
您也可以将PHP 绑定用于 LIBSSH2和 PHP。它应该适当地安装在系统上。
In Ubuntu 10.04 and Debian Lenny, of course with all dependences
在 Ubuntu 10.04 和 Debian Lenny 中,当然有所有依赖
apt-get install libssh2-php
回答by Igor
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 includethe public key within them).
Igor 建议的问题在于,除其他外,它的可移植性要低得多(libssh2 没有安装在很多主机上),它具有更直观的基于 OOP 的 API 和 RSA 身份验证实际上是有意义的(libssh2 需要您将公钥和私钥分别存储在文件系统上;必须单独提供它们的事实很愚蠢,因为大多数私钥格式都包含其中的公钥)。
phpseclib is also faster:
phpseclib 也更快:
http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/#comment_3759
http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/#comment_3759
回答by Farhan
$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";
}
}

