如何使用 PHP 进行 SFTP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4689540/
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
How to SFTP with PHP?
提问by indranama
I have came across many PHP scripts for web FTP clients. I need to implement a SFTP client as a web application in PHP. Does PHP support for SFTP? I couldn't find any samples. Can anyone help me with this?
我遇到过许多用于 Web FTP 客户端的 PHP 脚本。我需要在 PHP 中将 SFTP 客户端实现为 Web 应用程序。PHP 是否支持 SFTP?我找不到任何样品。谁能帮我这个?
回答by Gordon
PHP has ssh2 stream wrappers (disabled by default), so you can use sftp connections with any function that supports stream wrappers by using ssh2.sftp://
for protocol, e.g.
PHP 具有 ssh2 流包装器(默认情况下禁用),因此您可以将 sftp 连接与任何通过使用ssh2.sftp://
for 协议支持流包装器的函数一起使用,例如
file_get_contents('ssh2.sftp://user:[email protected]:22/path/to/filename');
or - when also using the ssh2 extension
或 - 当还使用ssh2 扩展时
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$sftp = ssh2_sftp($connection);
$stream = fopen("ssh2.sftp://$sftp/path/to/file", 'r');
See http://php.net/manual/en/wrappers.ssh2.php
见http://php.net/manual/en/wrappers.ssh2.php
On a side note, there is also quite a bunch of questions about this topic already:
顺便说一句,关于这个话题已经有很多问题了:
回答by calgaryhit
The ssh2 functions aren't very good. Hard to use and harder yet to install, using them will guarantee that your code has zero portability. My recommendation would be to use phpseclib, a pure PHP SFTP implementation.
ssh2 功能不是很好。难以使用且难以安装,使用它们将保证您的代码具有零可移植性。我的建议是使用phpseclib,一个纯 PHP SFTP 实现。
回答by Prut Udomwattawee
I found that "phpseclib" should help you with this (SFTP and many more features). http://phpseclib.sourceforge.net/
我发现“phpseclib”应该可以帮助您解决这个问题(SFTP 和更多功能)。http://phpseclib.sourceforge.net/
To Put the file to the server, simply call (Code example from http://phpseclib.sourceforge.net/sftp/examples.html#put)
要将文件放入服务器,只需调用(来自http://phpseclib.sourceforge.net/sftp/examples.html#put 的代码示例)
<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}
// puts a three-byte file named filename.remote on the SFTP server
$sftp->put('filename.remote', 'xxx');
// puts an x-byte file named filename.remote on the SFTP server,
// where x is the size of filename.local
$sftp->put('filename.remote', 'filename.local', NET_SFTP_LOCAL_FILE);
回答by Salines
Install Flysystem:
安装飞行系统:
composer require league/flysystem-sftp
Then:
然后:
use League\Flysystem\Filesystem;
use League\Flysystem\Sftp\SftpAdapter;
$filesystem = new Filesystem(new SftpAdapter([
'host' => 'example.com',
'port' => 22,
'username' => 'username',
'password' => 'password',
'privateKey' => 'path/to/or/contents/of/privatekey',
'root' => '/path/to/root',
'timeout' => 10,
]));
$filesystem->listFiles($path); // get file lists
$filesystem->read($path_to_file); // grab file
$filesystem->put($path); // upload file
....
Read:
读:
回答by DaveyBoy
I performed a full-on cop-out and wrote a class which creates a batch file and then calls sftp
via a system
call. Not the nicest (or fastest) way of doing it but it works for what I need and it didn't require any installation of extra libraries or extensions in PHP.
我执行了全面的 cop-out 并编写了一个创建批处理文件的类,然后sftp
通过调用进行system
调用。不是最好(或最快)的方法,但它可以满足我的需要,并且不需要在 PHP 中安装任何额外的库或扩展。
Could be the way to go if you don't want to use the ssh2
extensions
如果您不想使用ssh2
扩展程序,可能是要走的路